<?php
require_once '../../application/Config.php';
require_once '../../application/Db.php';
require_once '../../application/SPDO.php';

/****************************************
 *
 *  Author : Miguel Vasquez
 *  File   : classAjax.php
 *  Class AjaxRequest
 *  This class has the function, insert, edit
 *  and get data using ajax
 *
 **************************************/

class AjaxRequest
{
    /*
	 * --------------------
	 * @db DateBase
	 * @_dateNow Date now
	 * --------------------
	 */
    protected $db;
    private $_dateNow;

    public function __construct()
    {
        /*
		 * --------------------
		 * Database Connection
		 * --------------------
		 */

        $this->db = SPDO::singleton();
        $this->_dateNow = date('Y-m-d G:i:s', time());
    }

    public function getSettings()
    {
        /*
		 * -----------------------
		 * get Settings from Admin
		 * -----------------------
		 */
        $post = $this->db->query("
        SELECT
        title,
        description,
        keywords,
        message_length,
        post_length,
        ad
        FROM admin_settings ");
        return $post->fetch(PDO::FETCH_OBJ);
    }

    public function getPhotoPost()
    {
        /*
		 * --------------------------------------
		 * get photo of post, when delete an post
		 * --------------------------------------
		 */

        $query = $this->db->prepare("
		SELECT photo FROM posts WHERE id = :id_post && user = :user");
        $query->execute(array(':id_post' => $_POST['id'], ':user' => $_SESSION['authenticated']));
        return $query->fetch(PDO::FETCH_OBJ);
        $this->db = null;
    }

    public function infoUserLive($usr)
    {
        /*
		 * --------------------------------------------
		 * get info user in live, name, username, etc..
		 * -------------------------------------------
		 */
        if (isset($usr)) {
            $sql = "
			SELECT 
			U.id,
			U.username,
			U.name,
			U.avatar,
			U.email,
			U.type_account,
			U.password,
			U.bio,
			U.mode,
			U.email_notification_follow,
			U.email_notification_msg,
			U.language,
			U.type_account,
			P.bg,
			P.bg_position,
			P.bg_attachment,
			P.color_link,
			P.bg_color,
			P.cover_image
			FROM users U
			LEFT JOIN profile_design P ON U.id = P.user 
			WHERE U.id = ?
			";

            $data = $this->db->prepare($sql);

            if ($data->execute(array($usr))) {

                return $data->fetch(PDO::FETCH_OBJ);
                $this->db = null;
            }

        }
        // END ISSET
    }

    //<--- * END FUNCTION *-->

    public function checkRepost($id, $user)
    {
        /*
		 * ----------------------------------------------
		 * Repost of User
		 * ----------------------------------------------
		 */
        $id = (int)$id;

        $sql = $this->db->prepare("SELECT id FROM posts WHERE repost_of_id = ? && user = ? && status = '1' ");
        $sql->execute(array($id, $user));
        $response = $sql->fetch();

        if (!empty($response)) {
            return true;
        } else {
            return false;
        }
    }

    public function getMaxId($id, $table)
    {
        /*
		 * @ $id : NAME ROW ID
		 * @ $table : NAME TABLE
		 * */
        $sql = $this->db->prepare(" SELECT MAX(" . $id . ") FROM " . $table . " ");

        if ($sql->execute(array($id, $table))) {
            while ($res = $sql->fetch()) {
                $maxId = $res[0];
            }
            return $maxId;
            $this->db = null;
        }
    }

    public function signUp()
    {
        /*
		 * ----------------------------------------------
		 * sign up
		 * 1) Verified username, if is available
		 * insert into database
		 * ----------------------------------------------
		 */
        $verifiedUsername = self :: checkUsername($_POST['username']) ? 1 : 0;

        if ($verifiedUsername == 1) {
            return (2);
        }

        /*
		 * -----------------------
		 * Insert User
		 * -----------------------
		 */
        $sql = "
	    INSERT INTO users 
	    VALUES(
	    null,
	    ?,
	    ?,
	    '',
	    'xx',
	    ?,
	    ?,
	    '" . $this->_dateNow . "',
	    '',
	    '',
	    'avatar.png',
	    ?,
	    '0',
	    '1',
	    'pending',
	    '1',
	    '1',
	    '1',
	    '" . $_SESSION['lang'] . "',
	    ?,
	    ?
		);";



        $password = sha1($_POST['password']);

        $stmt = $this->db->prepare($sql);

        $stmt->bindValue(1, $_POST['username'], PDO::PARAM_STR);
        $stmt->bindValue(2, $_POST['full_name'], PDO::PARAM_STR);
        $stmt->bindValue(3, $password, PDO::PARAM_STR);
        $stmt->bindValue(4, $_POST['email'], PDO::PARAM_STR);
        $stmt->bindValue(5, $_POST['code'], PDO::PARAM_STR);
        $stmt->bindValue(6, 'male', PDO::PARAM_STR);
        $stmt->bindValue(7, time(), PDO::PARAM_INT);
        $stmt = $stmt->execute();

        /*
		 * -----------------------
		 * User ID inserted
		 * -----------------------
		 */
        $idUsr = $this->db->lastInsertId($stmt);

        if ($stmt == true) {

            //============================================================//
            //=                * INSERT PROFILE DESIGN *                 =//
            //============================================================//
            $profileDesign = $this->db->prepare("
			INSERT INTO profile_design
			VALUES
			(
			null,
			?,
			'0.jpg',
			'left',
			'fixed',
			'#0088E2',
			'#000000',
			''
			)");
            $profileDesign->execute(array($idUsr));

            return (1);

        }
        $this->db = null;
    }

    public function checkUsername($username)
    {

        /*
		 * -----------------------
		 * VERIFIED USERNAME
		 * -----------------------
		 */
        $post = $this->db->prepare("SELECT username FROM users WHERE username = :user");
        $post->execute(array(':user' => $username));
        return $post->fetchall();
        $this->db = null;
    }

    public function signIn()
    {
        /*
	 * -----------------------
	 * Sign in
	 * -----------------------
	 */
        $pass = sha1($_POST['password']);
        $user = $_POST['user'];

        $sql = "
   	 SELECT 
   	 id, 
   	 username,
   	 language
   	 FROM users
   	 WHERE username = :user && password = :pass && status = 'active' ||
   	 email = :user && password = :pass && status = 'active'";

        $query = $this->db->prepare($sql);
        if ($query->execute(
            array(
                ':user' => $user,
                ':pass' => $pass
            )
        )
        ) {
            return $query->fetch(PDO::FETCH_ASSOC);
        }
        $this->db = null;

    }

    public function loginAdmin()
    {
        /*
	 * -----------------------
	 * Log in Admin
	 * -----------------------
	 */

        $pass = sha1($_POST['pass']);
        $user = $_POST['usr'];

        $sql = "
   	 SELECT 
   	 id, 
   	 user,
   	 name
   	 FROM admin
   	 WHERE user = :user && pass = :pass && status = 'active'";

        $query = $this->db->prepare($sql);
        if ($query->execute(
            array(
                ':user' => $user,
                ':pass' => $pass
            )
        )
        ) {
            return $query->fetch(PDO::FETCH_ASSOC);
        }
        $this->db = null;

    }

    public function insertPost()
    {

        /*
	 * -----------------------
	 * Insert Post
	 * -----------------------
	 */
        $sql = "
   	 INSERT INTO posts 
   	 VALUES(
   	 null,
   	 0,
   	 ?,
   	 ?,
   	 ?,
   	 ?,
   	 ?,
   	 ?,
   	 ?,
   	 ?,
   	 '1',
   	 ?,
   	 '" . $this->_dateNow . "',
   	 '1',
   	 ?,
   	 ?,
   	 ?,
   	 ?,
   	 ?
   	 );
   	 ";

        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(1, $_POST['token_id'], PDO::PARAM_STR);
        $stmt->bindValue(2, $_POST['add_post'], PDO::PARAM_STR);
        $stmt->bindValue(3, $_POST['photoId'], PDO::PARAM_STR);
        $stmt->bindValue(4, $_POST['video_code'], PDO::PARAM_STR);
        $stmt->bindValue(5, $_POST['video_title'], PDO::PARAM_STR);
        $stmt->bindValue(6, $_POST['video_site'], PDO::PARAM_STR);
        $stmt->bindValue(7, $_POST['video_url'], PDO::PARAM_STR);
        $stmt->bindValue(8, $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->bindValue(9, $_POST['video_thumbnail'], PDO::PARAM_STR);
        $stmt->bindValue(10, $_POST['song'], PDO::PARAM_STR);
        $stmt->bindValue(11, $_POST['song_title'], PDO::PARAM_STR);
        $stmt->bindValue(12, $_POST['thumbnail_song'], PDO::PARAM_STR);
        $stmt->bindValue(13, isset($_POST['category']) ? $_POST['category'] : 0 , PDO::PARAM_STR);
        $stmt->bindValue(14,  0 , PDO::PARAM_INT);

        $stmt = $stmt->execute();

        /*
		 * -----------------------
		 * Get ID Post
		 * -----------------------
		 */
        $idPost = $this->db->lastInsertId($stmt);

        if ($stmt == true) {
            /*
			 * -----------------------------
			 * Insert Hashtag in Database
			 * ----------------------------
			 */
            preg_match_all('~([#])([^\s#]+)~', $_POST['add_post'], $matches);
            foreach ($matches as $key) {
            }
            $numHashTag = count($matches[1]);

            for ($i = 0; $i < $numHashTag; ++$i) {
                $query = $this->db->prepare("
						INSERT INTO trends_topic
						VALUES
						(
						?,
						?,
						'" . $this->_dateNow . "'
						)");
                $query->execute(array($_SESSION['authenticated'], $key[$i]));
            }

            /*--------------------
			 * SEND NOTIFICATION 
			 * ON MENTIONS
			 * ------------------
			 */

            $data_post = strtolower($_POST['add_post']);
            preg_match_all('~([@])([^\s@]+)~', $data_post, $_matches);

            foreach ($_matches as $_key) {
                $_key = array_unique($_key);
            }
            $_numMentions = count($_matches[1]);

            for ($j = 0; $j < $_numMentions; ++$j) {

                $_key[$j] = strip_tags($_key[$j]);

                /* Verified Username  */
                $ckUsername = self :: verifiedUsername(trim($_key[$j]));

                if (!empty($ckUsername)) {

                    if ($ckUsername->id != $_SESSION['authenticated']) {
                        /* Send Interaction */
                        self :: sendInteraction($ckUsername->id, $_SESSION['authenticated'], $idPost, 5);
                    }

                }
            }
            //<---- * END SEND NOTIFICATION ON MENTIONS * ----->

            /*--------------------
			 * Return ID of post
			 * ------------------
			 */
            return $idPost;
        }
        return 'Fail';
    }

    public function verifiedUsername($username)
    {
        /*
		 * -----------------------
		 * VERIFIED USERNAME
		 * -----------------------
		 */
        $post = $this->db->prepare("SELECT id FROM users WHERE username = :user");
        $post->execute(array(':user' => $username));
        return $post->fetch(PDO::FETCH_OBJ);
        $this->db = null;
    }

    public function sendInteraction($destination, $autor, $target, $type)
    {
        /*
		 * --------------------------------------------
		 * Insert user interactions.
		 * @$destination : ID Destiny
		 * @$autor : ID Autor
		 * @$target : Target General
		 * @type : Type of Interaction
		 * -------------------------------------------
		 */

        $sql = "
		INSERT INTO interactions
		VALUES(
		null,
		?,
		?,
		?,
		?,
		'0',
		'" . $this->_dateNow . "',
		'0'
		);
		";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(1, $destination, PDO::PARAM_INT);
        $stmt->bindValue(2, $autor, PDO::PARAM_INT);
        $stmt->bindValue(3, $target, PDO::PARAM_INT);
        $stmt->bindValue(4, $type, PDO::PARAM_INT);
        $stmt->execute();
    }

    public function checksendInteraction( $autor, $target, $type){
        $str = 'SELECT * FROM  interactions WHERE autor = '.$autor. ' AND target = '.$target. ' AND type = '.$type;
        $_sql = $this->db->prepare($str);
        $_sql->execute();
        if($_sql->rowCount() != 0)return 1;
        return 0;
    }

    public function deletePost()
    {
        /*
	 * -----------------------------
	 * Delete Post
	 * ----------------------------
	 */
        $sql = $this->db->prepare("UPDATE posts SET status = '0', `status_general` =  '0' WHERE id = :_id && token_id = :_token && user = :_user");
        $sql->execute(array(':_id' => $_POST['id'], ':_token' => $_POST['token'], ':_user' => $_POST['user']));

        if ($sql->rowCount() != 0) {

            /* Delete Repost */
            $_sql = $this->db->prepare("UPDATE posts SET status = '0', status_general = '0' WHERE repost_of_id = :_id && token_id = :_token");
            $_sql->execute(array(':_id' => $_POST['id'], ':_token' => $_POST['token']));

            //<-- DELETE INTERACTIONS
            $_sqlItera = $this->db->prepare("UPDATE interactions SET trash = '1' WHERE target = :_id && type != '1'");
            $_sqlItera->execute(array(':_id' => $_POST['id']));

            return (1);
        }
        $this->db = null;
    }

    public function favorites()
    {
        /*
	 * ----------------------------------------------------------------------------
	 *  Add, Remove Favorites
	 * @$active  :"Check to see if the user has already added prior to Favourites"
	 * @verified : "Check if the publication exists"
	 * ----------------------------------------------------------------------------
	 */

        $active = self :: favsUser(null, $_SESSION['authenticated'], $_POST['id']);
        $verified = self :: checkPost($_POST['id'], $_POST['token']) ? 1 : 0;


        if ($verified == 1 && empty($active)) {
            $_idPost = (int)$_POST['id'];
            $_sql = $this->db->prepare("SELECT user FROM posts WHERE id = :id");
            $_sql->execute(array(
                    ':id' => $_idPost
                )
            );
            $response = $_sql->fetch(PDO::FETCH_OBJ);

            /** If not exists, insert new record  **/
            $sql = $this->db->prepare("INSERT INTO favorites VALUES( null, ?, ?, '1', '" . $this->_dateNow . "' );");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['id'], PDO::PARAM_INT);
            $sql->execute();
            if ($sql->rowCount() != 0) {

                if ($response->user != $_SESSION['authenticated']) {
                    /* Send Interaction */
                    self :: sendInteraction($response->user, $_SESSION['authenticated'], $_idPost, 3);
                }
                return (1);
            }

        }

        if ($verified == 1 && !empty($active) && $active[0]['status'] == '1') {
            /** If exists, update status to Delete/Trash  **/
            $sql = $this->db->prepare("UPDATE favorites SET status = '0' WHERE id_usr = ? && id_favorite = ? ");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['id'], PDO::PARAM_INT);
            $sql->execute();
            if ($sql->rowCount() != 0) {
                return (2);
            }
        } else if ($verified == 1 && !empty($active) && $active[0]['status'] == '0') {
            /** If exists and status == Delete/Trash, update status to Active  **/
            $sql = $this->db->prepare("UPDATE favorites SET status = '1' WHERE id_usr = ? && id_favorite = ? ");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['id'], PDO::PARAM_INT);
            $sql->execute();
            if ($sql->rowCount() != 0) {
                return (3);
            }
        }
        return false;
        $this->db = null;

    }

    //<----------- end Method

    public function favsUser($where, $_session, $_id)
    {
        /*
		 * -----------------------------
		 * $_session : Current session
		 * $_id      : $id Post
		 * Favorites User
		 * ----------------------------
		 */
        $query = $this->db->prepare("
		SELECT 
		status
		FROM favorites
		WHERE id_usr = ? && 
		id_favorite = ?
		" . $where . "
		");
        $query->execute(array($_session, $_id));
        return $query->fetchall();
        $this->db = null;
    }

    public function checkPost($id, $token)
    {
        /*
		 * ----------------------------------------------
		 * know if publication exists in the database
		 * ----------------------------------------------
		 */
        $id = (int)$id;

        $sql = $this->db->prepare("SELECT id FROM posts WHERE id = ? && token_id = ? && status = '1' && repost_of_id = '0' ");
        $sql->execute(array($id, $token));
        $response = $sql->fetch();

        if (!empty($response)) {
            return true;
        } else {
            return false;
        }
    }

    //<-- end Method

    public function follow()
    {
        /*
	 * ----------------------------------------------------------------------------
	 * Follow, Unfollow users
	 * @$active  :"Check to see if the user has already follower"
	 * @verified : "Check if the user exists"
	 * ----------------------------------------------------------------------------
	 */

        $active = self :: checkFollow($_SESSION['authenticated'], $_POST['id']);
        $verified = self :: checkUser($_POST['id']) ? 1 : 0;

        if ($verified == 1 && empty($active)) {

            /** If not exists, insert new record  **/
            $sql = $this->db->prepare("INSERT INTO followers VALUES( null, ?, ?, '1', '" . $this->_dateNow . "' );");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['id'], PDO::PARAM_INT);
            $sql->execute();

            if ($sql->rowCount() != 0) {

                /* Send Interaction */
                self :: sendInteraction($_POST['id'], $_SESSION['authenticated'], $_SESSION['authenticated'], 1);
                return (1);
            }

        }

        if ($verified == 1 && !empty($active) && $active[0]['status'] == '1') {

            /** If exists, update status to Delete/Trash  **/
            $sql = $this->db->prepare("UPDATE followers SET status = '0' WHERE follower = ? && following = ? ");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['id'], PDO::PARAM_INT);
            $sql->execute();

            if ($sql->rowCount() != 0) {
                return (2);
            }
        } else if ($verified == 1 && !empty($active) && $active[0]['status'] == '0') {

            /** If exists and status == Delete/Trash, update status to Active  **/
            $sql = $this->db->prepare("UPDATE followers SET status = '1' WHERE follower = ? && following = ? ");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['id'], PDO::PARAM_INT);
            $sql->execute();

            if ($sql->rowCount() != 0) {
                return (3);
            }
        }
        return false;
        $this->db = null;
    }

    //<-- end Method

    public function checkFollow($follower, $following)
    {
        /*
		 * ----------------------------------------------
		 * Namely follow up status
		 * ----------------------------------------------
		 */
        $follower = (int)$follower;
        $following = (int)$following;

        $query = $this->db->prepare("
		SELECT status FROM followers WHERE follower = ? && following = ?");
        $query->execute(array($follower, $following));
        return $query->fetchall();
        $this->db = null;
    }

    //<-- end Method

    public function checkUser($id)
    {
        /*
		 * --------------------------------------------
		 * check if the user is valid
		 * @$id ID of user
		 * -------------------------------------------
		 */

        $id = (int)$id;
        $sql = $this->db->prepare("SELECT id FROM users WHERE id = :id && status = 'active'");
        $sql->execute(array(':id' => $id));
        $response = $sql->fetch();

        if (!empty($response)) {
            return true;
        } else {
            return false;
        }
    }

    //<-- end Method

    public function getAllPosts($_where, $_limit, $session)
    {
        /*
			 * -----------------------------
			 * Get all Post in "Index page"
			 * $_where : Condition
			 * $_limit : Limit of post
			 * $_session: Current Session
			 * -----------------------------
			 */
        $sql = $this->db->prepare(" 
			SELECT 
			COUNT(DISTINCT FA.id ) favoriteUser,
			U.username,
			P.repost_of_id,
			P.id,
			P.token_id,
			P.post_details,
			P.photo,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.user,
			P.date,
			P.url_soundcloud,
			P.title_soundcloud,
			U.id user_id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode,
			U2.id rp_id_user,
			U2.name rp_name,
			U2.username rp_username,
			U2.avatar rp_avatar,
			U2.type_account rp_type_account,
			U2.mode rp_mode,
			RP.id rp_id,
			RP.user rp_user
			FROM posts P 
			LEFT JOIN users U ON P.user = U.id
			LEFT JOIN posts RP ON P.repost_of_id = RP.id
			LEFT JOIN users U2 ON RP.user = U2.id
			LEFT JOIN favorites  FA ON RP.id = FA.id_favorite 
			&& FA.id_usr = :id 
			&& FA.status = '1' 
			|| P.id = FA.id_favorite 
			&& FA.id_usr = :id 
			&& FA.status = '1' 
			" . $_where . "  
			" . $_limit . "
			");


//    echo "
//			SELECT
//			COUNT(DISTINCT FA.id ) favoriteUser,
//			U.username,
//			P.repost_of_id,
//			P.id,
//			P.token_id,
//			P.post_details,
//			P.photo,
//			P.video_code,
//			P.video_title,
//			P.video_site,
//			P.video_url,
//			P.user,
//			P.date,
//			P.url_soundcloud,
//			P.title_soundcloud,
//			U.id user_id,
//			U.name,
//			U.username,
//			U.avatar,
//			U.type_account,
//			U.mode,
//			U2.id rp_id_user,
//			U2.name rp_name,
//			U2.username rp_username,
//			U2.avatar rp_avatar,
//			U2.type_account rp_type_account,
//			U2.mode rp_mode,
//			RP.id rp_id,
//			RP.user rp_user
//			FROM posts P
//			LEFT JOIN users U ON P.user = U.id
//			LEFT JOIN posts RP ON P.repost_of_id = RP.id
//			LEFT JOIN users U2 ON RP.user = U2.id
//			LEFT JOIN favorites  FA ON RP.id = FA.id_favorite
//			&& FA.id_usr = :id
//			&& FA.status = '1'
//			|| P.id = FA.id_favorite
//			&& FA.id_usr = :id
//			&& FA.status = '1'
//			" . $_where . "
//			" . $_limit . "
//			";



        if ($sql->execute(array(':id' => $session))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }

    }

    //<-- end Method

    public function discover($_where, $_limit, $session)
    {
        /*
			 * --------------------------------
			 * Get all Post in "Discover Page"
			 * $_where : Condition
			 * $_limit : Limit of post
			 * $_session: Current Session
			 * --------------------------------
			 */
        $sql = $this->db->prepare("
			SELECT
			COUNT(DISTINCT FA.id ) favoriteUser,
			COUNT(DISTINCT F.id ) followActive,
			COUNT(DISTINCT B.id) blockUser,
			P.id,
			P.token_id,
			P.post_details,
			P.photo,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.user,
			P.date,
			P.url_soundcloud,
			P.title_soundcloud,
			U.id user_id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode
			FROM posts P
			LEFT JOIN users U ON P.user = U.id
			LEFT JOIN followers F ON P.user = F.following && F.follower = :id && F.status = '1'
			LEFT JOIN favorites  FA ON P.id = FA.id_favorite && FA.id_usr = :id && FA.status = '1'
			LEFT JOIN block_user B ON U.id = B.user && B.user_blocked = :id && B.status = '1'
			" . $_where . "
			
			" . $_limit . "
			");
        if ($sql->execute(array(':id' => $session))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }



    }

    public function get_post_meta($post_id)
    {
        $sql = $this->db->prepare("
			SELECT p.user as user, pm.*
			FROM post_meta pm, posts p
		    WHERE post_id = " . $post_id. " AND p.id = pm.post_id");

        $sql->execute();
        return $sql->fetchall(PDO::FETCH_ASSOC);
    }

    //<-- end

    public function connect($_where, $_limit, $session)
    {
        /*
			 * ------------------------------------
			 * Get all Mentions in "Connect page"
			 * $_where : Condition
			 * $_limit : Limit of post
			 * $_session: Current Session
			 * ------------------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			COUNT( DISTINCT FA.id ) favoriteUser,
			P.id,
			P.token_id,
			P.post_details,
			P.photo,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.user,
			P.date,
			P.url_soundcloud,
			P.title_soundcloud,
			U.id user_id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode
			FROM posts P
			LEFT JOIN users U ON P.user = U.id
			LEFT JOIN favorites FA ON P.id = FA.id_favorite && FA.id_usr = :id && FA.status = '1'
			" . $_where . "
			GROUP BY P.id
			ORDER BY P.id DESC
			" . $_limit . "
			");

        if ($sql->execute(
            array(
                ':id' => $session
            )
        )
        ) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }

    }

    //<-- end

    public function mentionsInReply($_where)
    {
        /*
			 * ------------------------------------
			 * Get all Mentions in Reply "Connect page"
			 * $_where : Condition
			 * ------------------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			P.id,
			P.token_id,
			P.post_details,
			P.photo,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.user,
			P.date,
			U.id user_id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode,
			UR.name r_name,
			UR.username r_username,
			UR.avatar r_avatar,
			UR.type_account r_type_account,
			UR.mode r_mode,
			R.date r_date,
			R.reply
			FROM posts P
			LEFT JOIN users U ON P.user = U.id
			LEFT JOIN reply R ON P.id = R.post && R.status = '1'
			LEFT JOIN users UR ON R.user = UR.id
			" . $_where . "
			GROUP BY R.id
			ORDER BY R.id DESC
			");

        if ($sql->execute()) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }

    }

    //<--- * END FUNCTION *-->

    public function getInteractions($where, $limit, $_id)
    {
        /*
			 * -----------------------------
			 * Get All interactions
			 * $where : Condition
			 * $limit : Limit media
			 * ---------------------------
			 */

        $sql = $this->db->prepare("
			SELECT 
			COUNT(DISTINCT R.id) come,
			I.id idInteraction,
			I.type,
			I.date,
			P.id,
			P.token_id,
			P.post_details,
			P.photo,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.user,
			P.url_soundcloud,
			P.title_soundcloud,
			P.status,
			U.id user_id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode,
			US.username p_username,
			R.reply
			FROM interactions I 
			LEFT JOIN users U ON I.autor = U.id
			LEFT JOIN posts P ON I.target = P.id && P.status = '1'
			LEFT JOIN reply R ON I.target = R.post 
			&& R.user = U.id 
			&& R.post = P.id 
			&& R.status = '1'
			LEFT JOIN followers F ON F.follower = U.id
			LEFT JOIN users US ON P.user = US.id
			LEFT JOIN favorites FA ON P.id = FA.id_favorite && I.target = FA.id_favorite
		 	WHERE I.destination = :id && I.autor != :id && " . $where . " && I.trash = '0'
		 	GROUP BY P.id DESC
		 	ORDER BY I.id DESC
			" . $limit . "
			");

        if ($sql->execute(array(':id' => $_id))) {

            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
    }

    //<--- * END FUNCTION *-->
    //get post_meta

    public function getPostsFavs($_where, $_limit, $session)
    {
        /*
			 * -----------------------------
			 * Get Post Favorites
			 * $_where : Condition
			 * $_limit : Limit of post
			 * $_session: Current Session
			 * -----------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			COUNT(DISTINCT FA.id ) favoriteUser,
			P.id,
			P.token_id,
			P.photo,
			P.post_details,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.user,
			P.date,
			P.url_soundcloud,
			P.title_soundcloud,
			U.id id_user,
			U.name,
			U.username,
			U.avatar,
			U.type_account
			FROM posts P
			LEFT JOIN users U ON P.user = U.id
			LEFT JOIN favorites F ON P.id = F.id_favorite && F.status = '1'
			LEFT JOIN favorites  FA ON P.id = FA.id_favorite && FA.id_usr = :id && FA.status = '1'
			" . $_where . " && U.status = 'active' && P.status = '1' && F.status = '1'
			GROUP BY P.id
			ORDER BY F.id DESC
			" . $_limit . "
			");

        if ($sql->execute(array(':id' => $session))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }

    }

    //end get post_meta

    public function getFollowers($_where, $_limit, $session)
    {
        /*
			 * -----------------------------
			 * Get Followers
			 * $_where : Condition
			 * $_limit : Limit of post
			 * $_session: Current Session
			 * -----------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			COUNT(DISTINCT FO.id ) followActive, 
			F.id id_follow,
			U.id,
			U.username,
			U.name,
			U.avatar,
			U.type_account,
			U.bio
			FROM users U
			LEFT JOIN followers F ON U.id = F.follower
			LEFT JOIN followers  FO ON U.id = FO.following && FO.follower = :id && FO.status = '1'
			" . $_where . " && U.status = 'active' && F.status = '1'
			GROUP BY U.id
			ORDER BY F.id DESC
			" . $_limit . "
			");

        if ($sql->execute(array(':id' => $session))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }

    }

    //<--- * END FUNCTION *-->

    public function getFollowing($_where, $_limit, $session)
    {
        /*
			 * -----------------------------
			 * Get Following
			 * $_where : Condition
			 * $_limit : Limit of post
			 * $_session: Current Session
			 * -----------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			COUNT(DISTINCT FO.id ) followActive,
			F.id id_follow,
			U.id,
			U.username,
			U.name,
			U.avatar,
			U.type_account,
			U.bio
			FROM users U
			LEFT JOIN followers F ON U.id = F.following
			LEFT JOIN followers  FO ON U.id = FO.following && FO.follower = :id && FO.status = '1'
			" . $_where . " && U.status = 'active' && F.status = '1'
			GROUP BY U.id
			ORDER BY F.id DESC
			" . $_limit . "
			");

        if ($sql->execute(array(':id' => $session))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }

    }

    function getPostByFollowId($followId = array(),$limit = 30){
        $where  = ' WHERE ';
        if(!empty($followId)){
            $i = 0;
            foreach($followId as $id){
                $i++;
                $ext = "";
                if($i > 1){
                    $ext = " OR ";
                }
                $where .= $ext." user = '".$id."' ";

            }

        }
        $sql = $this->db->prepare("
			SELECT * FROM posts  ".$where." LIMIT ".$limit);
        if ($sql->execute()) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }


    }

    //<--- * END FUNCTION *-->

    public function getMedia($_id, $_token)
    {
        /*
			 * -------------------------------------------
			 * Get photos / videos of the publications
			 * $_id : ID Publication/Post
			 * $_token : Token unique of Publication/Post
			 * -------------------------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			P.id,
			P.token_id,
			P.photo,
			P.post_details,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.user,
			P.date,
			P.url_soundcloud,
			P.title_soundcloud,
			U.username
			FROM posts P
			LEFT JOIN users U ON P.user = U.id
			WHERE P.id = :id  && P.status = '1'
			");

        if ($sql->execute(array(':id' => $_id))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
    }

    //<--- * END FUNCTION *-->

    public function getFavorites($_id)
    {
        /*
			 * ------------------------
			 * Get Favorites
			 * $_id : ID Publication/Post
			 * ----------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			U.id,
			U.username,
			U.name,
			U.avatar
			FROM users U
			LEFT JOIN favorites F ON U.id = F.id_usr && F.status = '1'
			LEFT JOIN posts P ON F.id_favorite = P.id
			WHERE P.id = :id
			ORDER BY F.date ASC
			LIMIT 10
			");

        if ($sql->execute(array(':id' => $_id))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
    }

    //<--- * END FUNCTION *-->

    public function getRepostUser($_id)
    {
        /*
			 * ------------------------
			 * Get Repost
			 * $_id : ID Publication/Post
			 * ----------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			COUNT( DISTINCT id ) totalRepost
			FROM posts
			WHERE repost_of_id = :id && status = '1'
			ORDER BY id ASC
			");

        if ($sql->execute(array(':id' => $_id))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
    }

    //<--- * END FUNCTION *-->

    public function getReply($_id,  $page = 1)
    {
        /*
			 * ------------------------
			 * Get Replys
			 * $_id : ID Publication/Post
			 * ----------------------
			 */
        /**
         *
         * pagging
         */
        $sql = $this->db->prepare("
			SELECT
			U.id,
			U.username,
			U.name,
			U.avatar,
			U.type_account,
			R.id idReply,
			R.reply,
			R.date
			FROM users U
			LEFT JOIN reply R ON U.id = R.user && R.status = '1'
			LEFT JOIN posts P ON R.post = P.id
			WHERE P.id = :id && R.status = '1'
			ORDER BY R.date DESC
			");
        $sql->execute(array(':id' => $_id));
        $total = count($sql->fetchall(PDO::FETCH_ASSOC));
        $limit = 5;
        $pages = ceil($total / $limit);
        $offset = ($page - 1)  * $limit;

        /**
         * end pagging
         *
         */
        $sql = $this->db->prepare("
			SELECT 
			U.id,
			U.username,
			U.name,
			U.avatar,
			U.type_account,
			R.id idReply,
			R.reply,
			R.date
			FROM users U
			LEFT JOIN reply R ON U.id = R.user && R.status = '1'
			LEFT JOIN posts P ON R.post = P.id
			WHERE P.id = :id && R.status = '1'
			ORDER BY R.date DESC
			LIMIT ".$offset.",".$limit."
			");

        if ($sql->execute(array(':id' => $_id))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
    }

    //<--- * END FUNCTION *-->

    public function sendReply()
    {
        /*
		 * -------------------------------
		 * Send Reply
		 * @checkPost : 
		 * Check if the publication exists
		 * -------------------------------
		 */
        $checkPost = self :: checkPost($_POST['id_reply'], $_POST['token_reply']) ? 1 : 0;

        if ($checkPost == 1) {

            /* -----------------
		 * Get ID user Post 
		 * ----------------
		 */
            $_idReply = (int)$_POST['id_reply'];
            $_sql = $this->db->prepare("SELECT user FROM posts WHERE id = :id");
            $_sql->execute(array(
                    ':id' => $_idReply
                )
            );
            $response = $_sql->fetch(PDO::FETCH_OBJ);


            $sql = "
			INSERT INTO reply
			VALUES
			(
			null,
			?,
			?,
			?,
			'" . $this->_dateNow . "',
			'1'
			)";

            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(1, $_POST['id_reply'], PDO::PARAM_INT);
            $stmt->bindValue(2, $_SESSION['authenticated'], PDO::PARAM_INT);
            $stmt->bindValue(3, $_POST['reply_post'], PDO::PARAM_INT);
            $stmt = $stmt->execute();

            /* Retrieve ID inserted */
            $id_reply = $this->db->lastInsertId($stmt);

            if ($stmt == true) {

                if ($response->user != $_SESSION['authenticated']) {

                    /*--------------------
			 * SEND NOTIFICATION 
			 * ON MENTIONS
			 * ------------------
			 */

                    $data_post = strtolower($_POST['reply_post']);
                    preg_match_all('~([@])([^\s@]+)~', $data_post, $_matches);

                    foreach ($_matches as $_key) {
                        $_key = array_unique($_key);
                    }
                    $_numMentions = count($_matches[1]);

                    for ($j = 0; $j < $_numMentions; ++$j) {

                        $_key[$j] = strip_tags($_key[$j]);

                        /* Verified Username  */
                        $ckUsername = self :: verifiedUsername(trim($_key[$j]));

                        if (!empty($ckUsername)) {

                            if ($ckUsername->id != $_SESSION['authenticated']) {
                                /* Send Interaction */
                                self :: sendInteraction($ckUsername->id, $_SESSION['authenticated'], $_POST['id_reply'], 6);
                            }

                        }
                    }
                    //<---- * END SEND NOTIFICATION ON MENTIONS * ----->

                    /* Send Interaction */
                    self :: sendInteraction($response->user, $_SESSION['authenticated'], $_POST['id_reply'], 4);
                }

                return $id_reply;
            }
        }
    }

    //<--- * END FUNCTION *-->

    public function deleteReply()
    {
        /*
		 * ------------------------
		 * Delete Reply
		 * ----------------------
		 */

        $sql = $this->db->prepare("UPDATE reply SET status = '0' WHERE id = :_id && user = :_user && status = '1' ");
        $sql->execute(array(':_id' => $_GET['id'], ':_user' => $_SESSION['authenticated']));

        if ($sql->rowCount() != 0) {
            return (1);
        } else {
            return false;
        }
        $this->db = null;
    }

    //<--- * END FUNCTION *-->

    public function reportPost()
    {
        /*
	 * ------------------------
	 * Report Post
	 * @$reported : verify if you have not reported
	 * @$verified: Check if the publication exists
	 * ----------------------
	 */

        $reported = self :: checkReportPost($_SESSION['authenticated'], $_POST['_postId']) ? 1 : 0;
        $verified = self :: checkPost($_POST['_postId'], $_POST['_token']) ? 1 : 0;

        if ($verified == 1) {
            if ($reported == 0) {
                $sql = $this->db->prepare("INSERT INTO post_reported VALUES( null, :_user, :_post, '" . $this->_dateNow . "' );");
                $sql->execute(array(':_user' => $_SESSION['authenticated'], ':_post' => $_POST['_postId']));

                if ($sql->rowCount() != 0) {
                    return (1);
                } else {
                    return false;
                }
                $this->db = null;
            } /* If the user already reported the publication */
            else {
                return (3);
            }

        } /* If no exists post */
        else {
            return (2);
        }
    }

    //<--- * END FUNCTION *-->

    public function checkReportPost($user, $reported)
    {
        /*
		 * ----------------------------------------------
		 * know whether a publication has been reported 
		 * by the user
		 * ----------------------------------------------
		 */
        $user = (int)$user;
        $reported = (int)$reported;

        $query = $this->db->prepare("
		SELECT id FROM post_reported WHERE user = ? && post_reported = ?");
        $query->execute(array($user, $reported));
        return $query->fetchall();
        $this->db = null;
    }

    //<--- * END FUNCTION *-->

    public function reportUser()
    {
        /*
	 * ------------------------
	 * Report User
	 * @$reported : verify if you have not reported
	 * @$verified: Check if the User exists
	 * ----------------------
	 */
        $reported = self::checkReportUser($_SESSION['authenticated'], $_POST['_userId']) ? 1 : 0;
        $verified = self::checkUser($_POST['_userId']) ? 1 : 0;

        if ($verified == 1) {
            if ($reported == 0) {
                $sql = $this->db->prepare("INSERT INTO users_reported VALUES( null, :_user, :_userId, '" . $this->_dateNow . "' );");
                $sql->execute(array(':_user' => $_SESSION['authenticated'], ':_userId' => $_POST['_userId']));

                if ($sql->rowCount() != 0) {
                    return (1);
                } else {
                    return false;
                }
                $this->db = null;
            } /* If the user already reported User */
            else {
                return (3);
            }

        } /* If no exists user */
        else {
            return (2);
        }
    }

    //<--- * END FUNCTION *-->	

    public function checkReportUser($user, $reported)
    {
        /*
		 * ----------------------------------------------
		 * Know whether a user has been reported
		 * ----------------------------------------------
		 */
        $user = (int)$user;
        $reported = (int)$reported;

        $query = $this->db->prepare("
		SELECT id FROM users_reported WHERE user = ? && id_reported = ?");
        $query->execute(array($user, $reported));
        return $query->fetchall();
        $this->db = null;
    }

    //<-- end Method

    public function blockUser()
    {
        /*
	 * ------------------------
	 * Block User
	 * @$reported : verify if you have not Blocked
	 * @$verified: Check if the user exists
	 * ----------------------
	 */

        $reported = self::checkUserBlock($_SESSION['authenticated'], $_POST['_userId']);
        $verified = self::checkUser($_POST['_userId']) ? 1 : 0;


        if ($verified == 1 && empty($reported)) {
            $sql = $this->db->prepare("INSERT INTO block_user VALUES( null, :_user, :_userId, '" . $this->_dateNow . "', '1' );");
            $sql->execute(array(':_user' => $_SESSION['authenticated'], ':_userId' => $_POST['_userId']));

            if ($sql->rowCount() != 0) {

                //================= UPDATE INTERACTIONS  STATUS -> DELETE ==============//
                $updateItera = "UPDATE interactions SET trash = '1' WHERE autor = ? && destination = ?";
                $_execSql = $this->db->prepare($updateItera);
                $_execSql->bindValue(1, $_POST['_userId'], PDO::PARAM_INT);
                $_execSql->bindValue(2, $_SESSION['authenticated'], PDO::PARAM_INT);
                $_execSql->execute();

                //================== UPDATE FOLLOWERS TO STATUS TRASH ============//
                $update = "UPDATE followers SET status = '0' 
					WHERE follower = ? && following = ?";
                $exec = $this->db->prepare($update);
                $exec->bindValue(1, $_POST['_userId'], PDO::PARAM_INT);
                $exec->bindValue(2, $_SESSION['authenticated'], PDO::PARAM_INT);
                $exec->execute();

                //================= UPDATE FOLLOWING TO STATUS TRASH ==============//
                $update2 = "UPDATE followers SET status = '0' 
					WHERE follower = ? && following = ?";
                $exe = $this->db->prepare($update2);
                $exe->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
                $exe->bindValue(2, $_POST['_userId'], PDO::PARAM_INT);
                $exe->execute();

                //================= UPDATE MESSAGES ==============//
                $updateMsg = "UPDATE messages SET remove_from = '0' 
					WHERE `from` = :user_1 && `to` = :user_2
					|| `to` = :user_1 && `from` = :user_2";
                $_exe = $this->db->prepare($updateMsg);
                $_exe->bindValue(':user_1', $_SESSION['authenticated'], PDO::PARAM_INT);
                $_exe->bindValue(':user_2', $_POST['_userId'], PDO::PARAM_INT);
                $_exe->execute();

                return (1);
            }
        }

        if ($verified == 1 && !empty($reported) && $reported[0]['status'] == '1') {
            $sql = $this->db->prepare("UPDATE block_user SET status = '0' WHERE user = ? && user_blocked = ?");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['_userId'], PDO::PARAM_INT);
            $sql->execute();

            if ($sql->rowCount() != 0) {
                //================== UPDATE FOLLOWERS TO STATUS ACTIVE "IF EXISTS" ============//
                $update = "UPDATE followers SET status = '1' 
					WHERE follower = ? && following = ?";
                $exec = $this->db->prepare($update);
                $exec->bindValue(1, $_POST['_userId'], PDO::PARAM_INT);
                $exec->bindValue(2, $_SESSION['authenticated'], PDO::PARAM_INT);
                $exec->execute();

                //================= UPDATE FOLLOWING  TO STATUS ACTIVE "IF EXISTS" ==============//
                $update2 = "UPDATE followers SET status = '1' 
					WHERE follower = ? && following = ?";
                $exe = $this->db->prepare($update2);
                $exe->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
                $exe->bindValue(2, $_POST['_userId'], PDO::PARAM_INT);
                $exe->execute();

                return (2);
            }
        } else if ($verified == 1 && !empty($reported) && $reported[0]['status'] == '0') {
            $sql = $this->db->prepare("UPDATE block_user SET status = '1' WHERE user = ? && user_blocked = ? ");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['_userId'], PDO::PARAM_INT);
            $sql->execute();

            if ($sql->rowCount() != 0) {
                //================== UPDATE FOLLOWERS TO STATUS TRASH ============//
                $update = "UPDATE followers SET status = '0' 
					WHERE follower = ? && following = ?";
                $exec = $this->db->prepare($update);
                $exec->bindValue(1, $_POST['_userId'], PDO::PARAM_INT);
                $exec->bindValue(2, $_SESSION['authenticated'], PDO::PARAM_INT);
                $exec->execute();

                //================= UPDATE FOLLOWING  TO STATUS TRASH ==============//
                $update2 = "UPDATE followers SET status = '0' 
					WHERE follower = ? && following = ?";
                $exe = $this->db->prepare($update2);
                $exe->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
                $exe->bindValue(2, $_POST['_userId'], PDO::PARAM_INT);
                $exe->execute();

                return (3);

            }
        }
        return false;
        $this->db = null;
    }

    //<<<-- End Function -->>>

    public function checkUserBlock($user, $blocked)
    {
        /*
		 * ----------------------------------------------
		 * Know whether a user has been blocked
		 * ----------------------------------------------
		 */
        $user = (int)$user;
        $blocked = (int)$blocked;

        $query = $this->db->prepare("
		SELECT status FROM block_user WHERE user = ? && user_blocked = ?");
        $query->execute(array($user, $blocked));
        return $query->fetchall();
        $this->db = null;
    }

    //<<<-- End Function -->>>

    public function updateProfile()
    {
        /*
		 * ------------------------
		 * Update Name, Location, 
		 * website, Bio in Profile
		 * ----------------------
		 */
        $sql = "UPDATE users SET name = :name, location= :location, website = :web, bio = :bio WHERE id = :user";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':name', $_POST['name'], PDO::PARAM_STR);
        $stmt->bindValue(':location', $_POST['location'], PDO::PARAM_STR);
        $stmt->bindValue(':web', $_POST['website'], PDO::PARAM_STR);
        $stmt->bindValue(':bio', $_POST['bio'], PDO::PARAM_STR);
        $stmt->bindValue(':user', $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->execute();

        if ($stmt->rowCount() != 0) {
            return true;
        } else {
            return false;
        }
        $this->db = null;
    }

    //<<<-- End Function -->>>

    public function updateSettings()
    {
        /*
		 * ------------------------
		 * Update username, mode, 
		 * country, Bio in Settings
		 * @$verified : Check username availability
		 * ----------------------
		 */
        $verified = self::checkUsername($_POST['username']) ? 1 : 0;

        /* If the username is not available */
        if ($verified == 1) {
            return (2);
        }

        /* Update if the username is true */
        if ($_POST['username'] != '' && $_POST['email'] == '') {
            $sql = "UPDATE users 
			SET username = :username, 
			mode = :mode, 
			country = :country, 
			messages_private = :msg,
			email_notification_follow = :notiFollow,
			email_notification_msg = :notiMsg,
			language = :lang
			WHERE id = :user";
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':username', $_POST['username'], PDO::PARAM_STR);
            $stmt->bindValue(':mode', $_POST['mode'], PDO::PARAM_INT);
            $stmt->bindValue(':country', $_POST['country'], PDO::PARAM_STR);
            $stmt->bindValue(':user', $_SESSION['authenticated'], PDO::PARAM_INT);
            $stmt->bindValue(':msg', $_POST['msg_private'], PDO::PARAM_INT);
            $stmt->bindValue(':notiFollow', $_POST['check_1'], PDO::PARAM_STR);
            $stmt->bindValue(':notiMsg', $_POST['check_0'], PDO::PARAM_STR);
            $stmt->bindValue(':lang', $_POST['lang'], PDO::PARAM_STR);
            $stmt->execute();

            if ($stmt->rowCount() != 0) {
                return (1);
            } else {
                return (3);
            }
        }

        /* Update if the e-mail is true */
        if ($_POST['email'] != '' && $_POST['username'] == '') {
            $sql = "UPDATE users 
		SET email= :email,
		mode = :mode, 
		country = :country, 
		messages_private = :msg,
		email_notification_follow = :notiFollow,
		email_notification_msg = :notiMsg,
		language = :lang
		WHERE id = :user";
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':email', $_POST['email'], PDO::PARAM_STR);
            $stmt->bindValue(':mode', $_POST['mode'], PDO::PARAM_INT);
            $stmt->bindValue(':country', $_POST['country'], PDO::PARAM_STR);
            $stmt->bindValue(':user', $_SESSION['authenticated'], PDO::PARAM_INT);
            $stmt->bindValue(':msg', $_POST['msg_private'], PDO::PARAM_INT);
            $stmt->bindValue(':notiFollow', $_POST['check_1'], PDO::PARAM_STR);
            $stmt->bindValue(':notiMsg', $_POST['check_0'], PDO::PARAM_STR);
            $stmt->bindValue(':lang', $_POST['lang'], PDO::PARAM_STR);
            $stmt->execute();

            if ($stmt->rowCount() != 0) {
                return (1);
            } else {
                return false;
            }
        }
        /* Update if the username and e-mail is true */
        if ($_POST['email'] != '' && $_POST['username'] != '') {
            $sql = "UPDATE users 
				SET username = :username, 
				email= :email, 
				mode = :mode, 
				country = :country, 
				messages_private = :msg,
				email_notification_follow = :notiFollow,
				email_notification_msg = :notiMsg,
				language = :lang
				WHERE id = :user";
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':username', $_POST['username'], PDO::PARAM_STR);
            $stmt->bindValue(':email', $_POST['email'], PDO::PARAM_STR);
            $stmt->bindValue(':mode', $_POST['mode'], PDO::PARAM_INT);
            $stmt->bindValue(':country', $_POST['country'], PDO::PARAM_STR);
            $stmt->bindValue(':user', $_SESSION['authenticated'], PDO::PARAM_INT);
            $stmt->bindValue(':msg', $_POST['msg_private'], PDO::PARAM_INT);
            $stmt->bindValue(':notiFollow', $_POST['check_1'], PDO::PARAM_STR);
            $stmt->bindValue(':notiMsg', $_POST['check_0'], PDO::PARAM_STR);
            $stmt->bindValue(':lang', $_POST['lang'], PDO::PARAM_STR);
            $stmt->execute();

            if ($stmt->rowCount() != 0) {
                return (1);
            } else {
                return (3);
            }
        }

        /* Update if the user name, e-mail are not defined */
        if ($_POST['email'] == '' && $_POST['username'] == '') {
            $sql = "UPDATE users 
				SET mode = :mode, 
				country = :country, 
				messages_private = :msg,
				email_notification_follow = :notiFollow,
				email_notification_msg = :notiMsg,
				language = :lang
				WHERE id = :user";
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':mode', $_POST['mode'], PDO::PARAM_INT);
            $stmt->bindValue(':country', $_POST['country'], PDO::PARAM_STR);
            $stmt->bindValue(':user', $_SESSION['authenticated'], PDO::PARAM_INT);
            $stmt->bindValue(':msg', $_POST['msg_private'], PDO::PARAM_INT);
            $stmt->bindValue(':notiFollow', $_POST['check_1'], PDO::PARAM_STR);
            $stmt->bindValue(':notiMsg', $_POST['check_0'], PDO::PARAM_STR);
            $stmt->bindValue(':lang', $_POST['lang'], PDO::PARAM_STR);
            $stmt->execute();

            if ($stmt->rowCount() != 0) {
                return (1);
            } else {
                return (3);
            }
        }


        $this->db = null;
    }

    public function uploadAvatar($img)
    {
        /*
		 * ------------------------
		 * Upload Avatar
		 * $img : New image user
		 * -----------------------
		 */
        $sql = "UPDATE users SET avatar = ? WHERE id = ?";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(1, $img, PDO::PARAM_STR);
        $stmt->bindValue(2, $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->execute();
        $this->db = null;
    }

    public function uploadCover($img)
    {
        /*
		 * ------------------------
		 * Upload Cover
		 * $img : New image user
		 * ----------------------
		 */
        $sql = "UPDATE profile_design SET cover_image = ? WHERE user = ?";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(1, $img, PDO::PARAM_STR);
        $stmt->bindValue(2, $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->execute();
        $this->db = null;
    }

    public function updateTheme($img)
    {
        /*
		 * ----------------------------
		 * Upload Theme or Background
		 * $img : New image user
		 * ----------------------------
		 */
        $sql = "UPDATE profile_design SET bg = ? WHERE user = ?";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(1, $img, PDO::PARAM_STR);
        $stmt->bindValue(2, $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->execute();
        $this->db = null;
    }

    public function bottomLess()
    {
        /*
		 * ------------------------
		 * Delete Background
		 * ----------------------
		 */
        $sql = "UPDATE profile_design SET bg = null WHERE user = ?";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->execute();
        $this->db = null;
    }

    public function updateDesignUser()
    {
        /*
		 * -----------------------------
		 * Setting: Background Position,
		 * attachment, Color Link,
		 * Color Background
		 * ---------------------------
		 */
        $sql = "UPDATE 
		profile_design 
		SET 
		bg_position = :pos, 
		bg_attachment = :attachment, 
		color_link = :link,
		bg_color = :color
		WHERE user = :user";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':pos', $_POST['pos'], PDO::PARAM_STR);
        $stmt->bindValue(':attachment', $_POST['mosaic'], PDO::PARAM_STR);
        $stmt->bindValue(':link', $_POST['link'], PDO::PARAM_STR);
        $stmt->bindValue(':color', $_POST['bg_color'], PDO::PARAM_STR);
        $stmt->bindValue(':user', $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->execute();
        $this->db = null;
    }

    public function getAllMedia($_id, $where, $limit)
    {
        /*
			 * -----------------------------
			 * Get All media
			 * $_id : ID User
			 * $where : Condition
			 * $limit : Limit media
			 * ---------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			P.id,
			P.photo,
			P.post_details,
			P.date,
			P.video_code,
			P.video_url,
			P.video_site,
			P.video_thumbnail,
			P.video_title,
			P.url_soundcloud,
			P.title_soundcloud,
			P.thumbnail_song
			FROM posts P
			WHERE P.user = :id && P.repost_of_id = '0' && P.status = '1' && P.photo != '' " . $where . " 
			|| P.user = :id && P.repost_of_id = '0' && P.status = '1' && P.video_code != '' " . $where . "
			|| P.user = :id && P.status = '1' && P.url_soundcloud != '' && repost_of_id = 0 " . $where . "
			ORDER BY P.id DESC
			" . $limit . "
			");

        if ($sql->execute(array(':id' => $_id))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
    }

    public function sendMessage()
    {
        /*
		 * ------------------------------------
		 * Send Message Private
		 * @$checkUser : Check if the user exists
		 * ------------------------------------
		 */

        $checkUser = self :: checkUser($_POST['id_user']) ? 1 : 0;

        if ($checkUser == 1) {
            /*-----------------------------------------------
			 * Verify that the recipient not is the sender
			 * ----------------------------------------------
			 */
            if ($_POST['id_user'] != $_SESSION['authenticated']) {
                $sql = "
				INSERT INTO messages
				VALUES
				(
				null,
				?,
				?,
				?,
				'" . $this->_dateNow . "',
				'new',
				'1'
				)";

                $stmt = $this->db->prepare($sql);
                $stmt->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
                $stmt->bindValue(2, $_POST['id_user'], PDO::PARAM_INT);
                $stmt->bindValue(3, $_POST['message'], PDO::PARAM_INT);
                $stmt->execute();

                if ($stmt->rowCount() != 0) {
                    return true;
                }
                $this->db = null;
            }
            //<<<--- * User != From * --->>>
        }
        //<<<--- * Check * --->>>

    }

    //<--- * END FUNCTION *-->

    public function sendMessageId()
    {
        /*
		 * -----------------------------
		 * Send Message Private ID
		 * @$checkUser : Check if the user exists
		 * ---------------------------
		 */
        $checkUser = self :: checkUser($_POST['id_reply']) ? 1 : 0;

        if ($checkUser == 1) {
            /*-----------------------------------------------
			 * Verify that the recipient not is the sender
			 * ----------------------------------------------
			 */
            if ($_POST['id_reply'] != $_SESSION['authenticated']) {
                $sql = "
				INSERT INTO messages
				VALUES
				(
				null,
				?,
				?,
				?,
				'" . $this->_dateNow . "',
				'new',
				'1'
				)";

                $stmt = $this->db->prepare($sql);
                $stmt->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
                $stmt->bindValue(2, $_POST['id_reply'], PDO::PARAM_INT);
                $stmt->bindValue(3, $_POST['reply_msg'], PDO::PARAM_INT);
                $stmt = $stmt->execute();

                //============ * ID MSG  * ========//
                $_idMsg = $this->db->lastInsertId($stmt);

                if ($stmt == true) {
                    return $_idMsg;
                }
                $this->db = null;
            }
            //<<<--- * User != From * --->>>
        }
        //<<<--- * Check * --->>>

    }

    //<<<--- * END FUNCTION *-->>>	

    public function deleteAccount()
    {
        /*
		 * -----------------------------
		 * Delete Account
		 * ---------------------------
		 */

        $sql = "UPDATE users SET status = 'delete' WHERE id = :id && status = 'active'";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':id', $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->execute();

        if ($stmt->rowCount() != 0) {

            //================= UPDATE INTERACTIONS  STATUS -> DELETE ==============//
            $updateItera = "UPDATE interactions SET trash = '1' WHERE autor = ?";
            $_execSql = $this->db->prepare($updateItera);
            $_execSql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $_execSql->execute();

            //================== UPDATE FOLLOWERS STATUS -> DELETE ============//
            $update = "UPDATE followers SET status = '0' WHERE follower = ?";
            $exec = $this->db->prepare($update);
            $exec->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $exec->execute();

            //================= UPDATE FOLLOWING  STATUS -> DELETE ==============//
            $update2 = "UPDATE followers SET status = '0' WHERE following = ?";
            $exe = $this->db->prepare($update2);
            $exe->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $exe->execute();

            //================= UPDATE FAVORITES  STATUS -> DELETE ==============//
            $update3 = "UPDATE favorites SET status = '0' WHERE id_usr = ?";
            $_exe = $this->db->prepare($update3);
            $_exe->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $_exe->execute();

            //================= UPDATE REPLY  STATUS -> DELETE ==============//
            $update4 = "UPDATE reply SET status = '0' WHERE user = ?";
            $_exec = $this->db->prepare($update4);
            $_exec->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $_exec->execute();

            //================= UPDATE POST  STATUS -> DELETE ==============//
            $update5 = "UPDATE posts SET status = '0' WHERE user = ?";
            $_exec_ = $this->db->prepare($update5);
            $_exec_->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);

            if ($_exec_->execute()) {

                /* 
				 */
                /* Get all photos of Publications/Posts */
                $querySql = $this->db->prepare("SELECT photo FROM posts WHERE user = ? && photo != '' ");

                if ($querySql->execute(array($_SESSION['authenticated']))) {

                    while ($row = $querySql->fetch(PDO::FETCH_ASSOC)) {
                        /*
						 * ------------------------------------------
						 * Delete all photos, publications you made.
						 * ------------------------------------------
						 */
                        $total = count($row);
                        $root = '../../upload/' . $row['photo'];

                        if ($total != 0) {
                            if (file_exists($root)) {
                                unlink($root);
                            }
                        }

                    }
                }
                //<--- * End Delete Photos Posts * --->

            }

            /*
			 * ----------------------------------------
			 * Delete Cover and Background
			 * ---------------------------------------
			 */
            $_querySql = $this->db->prepare("SELECT bg, cover_image 
		    FROM profile_design WHERE user = ?");

            if ($_querySql->execute(array($_POST['id']))) {
                $defaults = array('0.jpg', '1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg', '11.jpg');

                while ($row = $_querySql->fetch(PDO::FETCH_ASSOC)) {
                    $rootCover = '../cover/' . $row['cover_image'];
                    $rootCoverLarge = '../cover/large_' . $row['cover_image'];
                    $rootBg = '../backgrounds/' . $row['bg'];

                    //<<-- Cover
                    if (file_exists($rootCover) && $row['cover_image'] != '') {
                        unlink($rootCover);
                        unlink($rootCoverLarge); //<-- Large
                    }
                    //<<-- Background
                    if (file_exists($rootBg) && !in_array($row['bg'], $defaults) && $row['bg'] != '') {
                        unlink($rootBg);
                    }
                }
            }
            //<--- * End Delete Bg - Cover * --->

            /* Delete Avatar */
            $_querySqlAvatar = $this->db->prepare("SELECT avatar FROM user WHERE user = ?");
            if ($_querySqlAvatar->execute(array($_POST['id']))) {

                while ($rw = $_querySqlAvatar->fetch(PDO::FETCH_ASSOC)) {
                    $rootAvatar = '../avatar/' . $rw['avatar'];
                    $rootAvatarLarge = '../avatar/large_' . $rw['avatar'];

                    if (file_exists($rootAvatar)
                        && $rw['avatar'] != ''
                        && $rootAvatar != $rootAvatar . 'avatar.png'
                    ) {
                        unlink($rootAvatar); //<-- 62x62
                        unlink($rootAvatarLarge); //<-- Large
                    }
                }
            }
            //<<<-- End Delete Avtar -->>>

            return true;
        }
        $this->db = null;
    }

    //<<<--- * END FUNCTION *-->>>	

    public function updatePassword()
    {
        /*
		 * ----------------------------------------
		 *  Update Password 
		 *  from Page http://sitename.com/password/
		 * ----------------------------------------
		 */
        $pass = sha1($_POST['confirm']);
        $currentPass = sha1($_POST['current']);
        $sql = "UPDATE users SET password = :pass WHERE id = :user && password = :currentPass";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':pass', $pass, PDO::PARAM_STR);
        $stmt->bindValue(':currentPass', $currentPass, PDO::PARAM_STR);
        $stmt->bindValue(':user', $_SESSION['authenticated'], PDO::PARAM_INT);
        $stmt->execute();
        if ($stmt->rowCount() != 0) {
            return true;
        } else {
            return false;
        }
        $this->db = null;
    }

    public function updatePasswordRecover()
    {
        /*
		 * -------------------------------------------------
		 *  Update password Recover
		 *  from Page http://sitename.com/recover/ID_HASH
		 * -------------------------------------------------
		 */
        if (preg_match('/[^a-z0-9\_\-]/i', $_POST['idhash'])) {
            return false;
        }

        $pass = sha1($_POST['pass_2']);
        $sql = "UPDATE 
		users U 
		INNER JOIN recover_pass R ON U.email = R.email
		SET 
		U.password = :pass,
		R.verified = '1',
		R.date_update = '" . $this->_dateNow . "'
		WHERE R.id_hash = :idhash";

        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(':pass', $pass, PDO::PARAM_STR);
        $stmt->bindValue(':idhash', $_POST['idhash'], PDO::PARAM_INT);

        if ($stmt->execute()) {
            return true;
        } else {
            return false;
        }
        $this->db = null;
    }

    //<<< * End * -->>>>

    public function getMessages($_id, $where, $limit)
    {
        /*
		 * ----------------------------------------
		 * Get messages private
		 * $_id : ID User
		 * $where : Condition
		 * $limit : Limit Messages
		 * ---------------------------------------
		 */
        $sql = $this->db->prepare("
		SELECT 
	    mm.recipient_id,
	    m.*,
	    U.id id_user,
		U.username,
		U.name,
		U.type_account,
		U.avatar,
		U2.id id_2,
		U2.username username2,
		U2.name name2,
		U2.type_account type_account2,
		U2.avatar avatar2
		FROM (
		  SELECT
		  `from`,
		  `to`,
		    `from` + `to` - 1 as recipient_id,
		    MAX(id) as id
		  FROM
		    messages
		  WHERE
		    `from` = :id && remove_from = '1' " . $where . " ||
		    `to` = :id && remove_from = '1' " . $where . "
		  GROUP BY
		    `from` + `to` - 1
		  ) mm
		    INNER JOIN
		  messages m
		    On
		  mm.id = m.id
		  LEFT JOIN users U ON U.id = mm.`to`
		  LEFT JOIN users U2 ON U2.id = mm.`from` 
		  GROUP BY id_user, id_2
		  ORDER BY id DESC
  			" . $limit . "
		");
        if ($sql->execute(array(':id' => $_id))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
    }

    //<<< * End * -->>>>

    public function getMessageId($_from, $_to)
    {
        /*
		 * ----------------------------------------
		 * Get messages by ID
		 * $_from : User From/Sender
		 * $_to : User To/Addressee
		 * ---------------------------------------
		 */
        $sql = $this->db->prepare("
		SELECT 
		M.id,
		M.to,
		M.from,
		M.message,
		M.date,
		M.status,
		U.id id_user,
		U.username,
		U.name,
		U.type_account,
		U.avatar
		FROM messages M
		LEFT JOIN users U ON U.id = M.`from`
		WHERE `from` = :from && `to` = :to && remove_from = '1' || 
		`from` = :to && `to` = :from && remove_from = '1'
		GROUP BY M.id
		ORDER BY date ASC
		");
        if ($sql->execute(array(':from' => $_from, ':to' => $_to))) {
            //<<<--- Message Readed --->>>
            $_sql = $this->db->prepare("UPDATE messages SET status = 'readed' WHERE `from` = :_from && `to` = :_user ");
            $_sql->execute(array(':_from' => $_from, ':_user' => $_SESSION['authenticated']));

            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
    }

    //<<<<-- * End * --->>>>

    public function deleteMsg()
    {
        /*
	 * -------------------
	 * Delete Messages
	 * -------------------
	 */
        $sql = $this->db->prepare("UPDATE messages SET remove_from = '0' 
   	 WHERE id = :_id && `from` = :_user 
   	 && remove_from = '1' 
   	 || id = :_id 
   	 && `to` = :_user 
   	 && remove_from = '1'
   	 "
        );
        $sql->execute(array(':_id' => $_POST['_msgId'], ':_user' => $_SESSION['authenticated']));

        if ($sql->rowCount() != 0) {
            return (1);
        } else {
            return false;
        }
        $this->db = null;
    }

    //<<<<-- * End * --->>>>

    public function recoverPass()
    {
        /*
		 * ----------------------------------------
		 * Recover Pass
	     * @Verified : Check email is valid 
		 * ---------------------------------------
		 */
        $verified = self :: checkEmail($_POST['email_recover']) ? 1 : 0;

        if ($verified == 1) {
            /* If e-mail is True, We insert */
            $sql = "INSERT INTO recover_pass VALUES( null, :idHash, :email, '0', '" . $this->_dateNow . "', '' );";
            $stmt = $this->db->prepare($sql);
            $stmt->bindValue(':idHash', $_POST['id_hash'], PDO::PARAM_STR);
            $stmt->bindValue(':email', $_POST['email_recover'], PDO::PARAM_STR);
            $stmt->execute();

            if ($stmt->rowCount() != 0) {
                return true;
            } else {
                return false;
            }

        }
        //<<<---- If
        $this->db = null;


    }

    //<-- end Method

    public function checkEmail($mail)
    {
        /*
		 * --------------------------------------------
		 * check if the Email is valid
		 * @$mail Email of user
		 * -------------------------------------------
		 */
        $sql = $this->db->prepare("SELECT id FROM users WHERE email = :mail && status = 'active'");
        $sql->execute(array(':mail' => $mail));
        $response = $sql->fetch();

        if (!empty($response)) {
            return true;
        } else {
            return false;
        }
    }

    //<<<--- End Function

    public function search($data, $where, $group_order, $limit, $session)
    {
        /*
		 * ----------------------------------------
		 * Search #Hastag or some word
		 * $data    : Word to search
		 * $where   : Condition
		 * $limit   : Limit of records
		 * $session : Current Session
		 * ---------------------------------------
		 */

        $data = trim($data);
        $sql = "
			SELECT 
			COUNT(DISTINCT FA.id ) favoriteUser,
			COUNT(DISTINCT B.id) blockUser,
			P.id,
			P.token_id,
			P.post_details,
			P.photo,
			P.user,
			P.date,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.url_soundcloud,
			P.title_soundcloud,
			U.id user_id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode,
			MATCH( P.post_details, P.video_title, P.title_soundcloud ) AGAINST( '" . $data . "' IN BOOLEAN MODE ) relevance
			FROM posts P 
			LEFT JOIN users U ON P.user = U.id
			LEFT JOIN favorites  FA ON P.id = FA.id_favorite && FA.id_usr = :id && FA.status = '1'
			LEFT JOIN block_user B ON U.id = B.user && B.user_blocked = :id && B.status = '1'
			WHERE 
			P.post_details LIKE :query 
			&& U.mode = '1' " . $where . " 
			&& B.id IS NULL 
			&& P.repost_of_id = 0
			|| MATCH( P.post_details, P.video_title, P.title_soundcloud ) AGAINST( '" . $data . "' IN BOOLEAN MODE ) 
			&& P.repost_of_id = 0
			&& U.mode = '1' && B.id IS NULL " . $where . " 
			|| MATCH( P.post_details, P.video_title, P.title_soundcloud ) AGAINST( '" . $data . "*' IN BOOLEAN MODE )
			&& P.repost_of_id = 0 
			&& U.mode = '1' 
			&& B.id IS NULL " . $where . "
			" . $group_order . "
			" . $limit . "
			";
        $output = $this->db->prepare($sql);
        $output->bindValue(':query', '%' . $data . '%', PDO::PARAM_STR);
        $output->bindValue(':id', $session, PDO::PARAM_INT);


        if ($output->execute()) {
            return $output->fetchall(PDO::FETCH_ASSOC);
            $this->db = null;
        }

    }

    //<--- * END FUNCTION *-->

    public function searchUsers($data, $where, $limit, $session)
    {
        /*
			 * ----------------------------------------
			 * Search Users
			 * $data    : Word to search
			 * $where   : Condition
			 * $limit   : Limit of records
			 * $session : Current Session
			 * ---------------------------------------
			 */
        $data = trim($data, '@');
        $data = trim($data, '#');
        $sql = "
			SELECT 
			COUNT(DISTINCT FO.id ) followActive,
			COUNT(DISTINCT B.id) blockUser,
			U.bio,
			U.location,
			U.id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode
			FROM users U 
			LEFT JOIN followers FO ON U.id = FO.following && FO.follower = :id && FO.status = '1'
			LEFT JOIN block_user B ON U.id = B.user && B.user_blocked = :id && B.status = '1'
			WHERE  U.username LIKE :query 
			&& U.mode = '1' 
			&& B.id IS NULL " . $where . " 
			|| U.bio LIKE :query 
			&& U.mode = '1' 
			&& B.id IS NULL " . $where . " 
			|| U.name LIKE :query 
			&& U.mode = '1' 
			&& B.id IS NULL " . $where . " 
			GROUP BY U.id
			ORDER BY U.id DESC
			" . $limit . "
			";
        $output = $this->db->prepare($sql);
        $output->bindValue(':query', '%' . $data . '%', PDO::PARAM_STR);
        $output->bindValue(':id', $session, PDO::PARAM_INT);

        if ($output->execute()) {
            return $output->fetchall(PDO::FETCH_ASSOC);
            $this->db = null;
        }

    }

    //<--- * END FUNCTION *-->

    public function notificationsMessages()
    {
        /*
		 * -----------------------
		 * Get all news Messages
		 * ----------------------
		 */
        $sql = $this->db->prepare("
    	SELECT 
    	COUNT( DISTINCT `from` ) total
		FROM messages 
		WHERE `to` = :id && status = 'new' && remove_from = '1'
		GROUP BY `to`
    	");

        if ($sql->execute(array(':id' => $_SESSION['authenticated']))) {
            return $sql->fetch(PDO::FETCH_OBJ);
            $this->db = null;
        }
    }

    public function notificationsInteractions()
    {
        /*
		 * -----------------------
		 * Get all Interactions
		 * ----------------------
		 */
        $sql = $this->db->prepare("
    	SELECT 
    	COUNT(DISTINCT id ) total
		FROM interactions 
		WHERE destination = :id && status = '0'
    	");

        if ($sql->execute(array(':id' => $_SESSION['authenticated']))) {
            return $sql->fetch(PDO::FETCH_OBJ);
            $this->db = null;
        }
    }

    public function reposted()
    {
        /*
	 * ----------------------------------------------------------------------------
	 *  Resposted
	 * @$active  :"Check to see if the user has already repost"
	 * @verified : "Check if the publication exists"
	 * ----------------------------------------------------------------------------
	 */

        $active = self :: checkReposted($_POST['id'], $_SESSION['authenticated']);
        $verified = self :: checkPost($_POST['id'], $_POST['token']) ? 1 : 0;


        if ($verified == 1 && empty($active)) {
            $_idPost = (int)$_POST['id'];
            $_sql = $this->db->prepare("
		SELECT 
		post_details, 
		token_id,
		photo,
		video_code,
		video_title,
		video_site,
		video_url,
		user,
		video_thumbnail,
		date,
		url_soundcloud,
		title_soundcloud,
		thumbnail_song
		FROM posts 
		WHERE id = :id
		");
            $_sql->execute(array(
                    ':id' => $_idPost
                )
            );
            $response = $_sql->fetch(PDO::FETCH_OBJ);

            /** If not exists, insert new record  **/
            $sql = $this->db->prepare("INSERT INTO posts 
		VALUES( 
		null,
	   	 ?,
	   	 ?,
	   	 ?,
	   	 ?,
	   	 ?,
	   	 ?,
	   	 ?,
	   	 ?,
	   	 ?,
	   	 '1',
	   	 ?,
	   	 ?,
   		 '1',
   		 ?,
   		 ?,
   		 ?
		 );
		 ");
            $sql->bindValue(1, $_POST['id'], PDO::PARAM_INT);
            $sql->bindValue(2, $response->token_id, PDO::PARAM_STR);
            $sql->bindValue(3, '' . $response->post_details . '', PDO::PARAM_STR);
            $sql->bindValue(4, '' . $response->photo . '', PDO::PARAM_STR);
            $sql->bindValue(5, '' . $response->video_code . '', PDO::PARAM_STR);
            $sql->bindValue(6, '' . $response->video_title . '', PDO::PARAM_STR);
            $sql->bindValue(7, '' . $response->video_site . '', PDO::PARAM_STR);
            $sql->bindValue(8, '' . $response->video_url . '', PDO::PARAM_STR);
            $sql->bindValue(9, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(10, '' . $response->video_thumbnail . '', PDO::PARAM_STR);
            $sql->bindValue(11, $response->date, PDO::PARAM_STR);
            $sql->bindValue(12, $response->url_soundcloud, PDO::PARAM_STR);
            $sql->bindValue(13, $response->title_soundcloud, PDO::PARAM_STR);
            $sql->bindValue(14, $response->thumbnail_song, PDO::PARAM_STR);
            $sql->execute();

            if ($sql->rowCount() != 0) {

                if ($response->user != $_SESSION['authenticated']) {
                    /* Send Interaction */
                    self :: sendInteraction($response->user, $_SESSION['authenticated'], $_idPost, 2);
                }
                return (1);
            }

        }

        if ($verified == 1 && !empty($active) && $active[0]['status'] == '1') {
            /** If exists, update status to Delete/Trash  **/
            $sql = $this->db->prepare("UPDATE posts SET status = '0' WHERE user = ? && repost_of_id = ? ");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['id'], PDO::PARAM_INT);
            $sql->execute();
            if ($sql->rowCount() != 0) {
                return (2);
            }
        } else if ($verified == 1 && !empty($active) && $active[0]['status'] == '0') {
            /** If exists and status == Delete/Trash, update status to Active  **/
            $sql = $this->db->prepare("UPDATE posts SET status = '1' WHERE user = ? && repost_of_id = ? ");
            $sql->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_INT);
            $sql->bindValue(2, $_POST['id'], PDO::PARAM_INT);
            $sql->execute();
            if ($sql->rowCount() != 0) {
                return (3);
            }
        }
        return false;
        $this->db = null;

    }

    public function checkReposted($id, $user)
    {
        /*
		 * ----------------------------------------------
		 * Repost of User
		 * ----------------------------------------------
		 */
        $id = (int)$id;

        $sql = $this->db->prepare("SELECT status FROM posts WHERE repost_of_id = ? && user = ? ");
        $sql->execute(array($id, $user));
        return $sql->fetchall();
        $this->db = null;
    }

    //<-- end

    public function getTotalSummary($usr)
    {
        /*
		 * --------------------------------------------
		 * Get Total Followers, Followings and Post
		 * -------------------------------------------
		 */
        if (isset($usr)) {
            $sql = "
			SELECT 
			COUNT( DISTINCT FO.id ) totalFollowers,
			COUNT( DISTINCT FOL.id ) totalFollowing,
			COUNT( DISTINCT PO.id ) totalPosts
			FROM users U 
			LEFT JOIN posts PO ON U.id = PO.user && PO.status = '1' && PO.status_general = '1'
			LEFT JOIN followers FO ON U.id = FO.following && FO.status = '1'
			LEFT JOIN followers FOL ON U.id = FOL.follower && FOL.status = '1'
			LEFT JOIN favorites FA ON U.id = FA.id_usr && FA.status = '1'
			WHERE U.id = ?
			GROUP BY U.id
			";

            $data = $this->db->prepare($sql);
            if ($data->execute(array($usr))) {

                return $data->fetch(PDO::FETCH_OBJ);
                $this->dbh = null;
            }

        }
        // END ISSET
    }

    //<--- * END FUNCTION *-->


    //<<<--- * Delete All Messages * --->>>

    public function deleteAllMsg()
    {
        /*
	 * -------------------
	 * Delete All Messages
	 * -------------------
	 */
        $sql = $this->db->prepare("UPDATE messages SET remove_from = '0' 
   	 WHERE `to` = :_idFrom 
   	 && `from` = :_user 
   	 || `from` = :_idFrom 
   	 && `to` = :_user 
   	 "
        );
        $sql->execute(array(':_idFrom' => $_POST['_userId'], ':_user' => $_SESSION['authenticated']));

        if ($sql->rowCount() != 0) {
            return (1);
        } else {
            return false;
        }
        $this->db = null;
    }

    //<-- End Delete All Messages

    public function searchUsersMentions($data, $where, $limit, $session)
    {
        /*
			 * ----------------------------------------
			 * Search Users
			 * $data    : Word to search
			 * $where   : Condition
			 * $limit   : Limit of records
			 * $session : Current Session
			 * ---------------------------------------
			 */
        $data = trim($data, '@');
        $data = trim($data, '#');
        $sql = "
			SELECT 
			COUNT(DISTINCT FO.id ) followActive,
			COUNT(DISTINCT B.id) blockUser,
			U.bio,
			U.location,
			U.id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode
			FROM users U 
			LEFT JOIN followers FO ON U.id = FO.following && FO.follower = :id && FO.status = '1'
			LEFT JOIN block_user B ON U.id = B.user && B.user_blocked = :id && B.status = '1'
			WHERE  U.username LIKE :query 
			&& U.mode = '1' 
			&& B.id IS NULL " . $where . " 
			&& U.mode = '1' 
			&& B.id IS NULL " . $where . " 
			|| U.name LIKE :query 
			&& U.mode = '1' 
			&& B.id IS NULL " . $where . " 
			GROUP BY U.id
			ORDER BY U.type_account DESC
			" . $limit . "
			";
        $output = $this->db->prepare($sql);
        $output->bindValue(':query', '%' . $data . '%', PDO::PARAM_STR);
        $output->bindValue(':id', $session, PDO::PARAM_INT);

        if ($output->execute()) {
            return $output->fetchall(PDO::FETCH_ASSOC);
            $this->db = null;
        }

    }

    //<--- * END FUNCTION *-->

    //<<<<<<<<<<<<<<<<<<<<<<<< ---------- v2.6 ------------->>>>>>>>>>>>>>>>>>

    // Begin Method

    public function whoToFollow($_id)
    {
        /*
			 * -----------------------------
			 * "who To Follow" : 
			 * suggestion who to follow
			 * $id : Current user "Session ID"
			 * -----------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			COUNT(DISTINCT B.id) blockUser,
			U.id,
			U.name,
			U.username,
			U.avatar,
			U.type_account
			FROM users U
			LEFT JOIN followers F ON U.id = F.following && F.follower = :id && F.status = '1'
			LEFT JOIN block_user B ON U.id = B.user && B.user_blocked = :id && B.status = '1'
			WHERE U.id <> :id && F.id IS NULL && U.status = 'active' && B.id IS NULL
			GROUP BY U.id
			ORDER BY rand(" . time() . " * " . time() . ")
			LIMIT 3
			");

        //return $sql;
        if ($sql->execute(array(':id' => $_id))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
        $this->db = null;

    }

    //<--- * End Method *-->

    // Begin Method

    public function allUsername()
    {
        /*
			 * -----------------------------
			 * "all Username" : 
			 * show all users in sitemaps.xml
			 * -----------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			username
			FROM users
			");

        //return $sql;
        if ($sql->execute()) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
        $this->db = null;

    }

    //<--- * End Method *-->

    // Begin Method

    public function allPages()
    {
        /*
			 * -----------------------------
			 * "all Pages" : 
			 * show all Pages in sitemaps.xml
			 * -----------------------------
			 */
        $sql = $this->db->prepare("
			SELECT 
			url
			FROM pages_general
			");

        //return $sql;
        if ($sql->execute()) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }
        $this->db = null;

    }

    //<--- * End Method *-->


    /*insert post meta*/

    public function insertMeta($post_id)
    {

        $value = '';
        //rating
        if ($_POST['post_type'] == 4) {
           $value = json_encode(array("1","2","3","4","5"));
        } //slider
        elseif ($_POST['post_type'] == 3) {
            $min = $_POST['meta_value'][0];
            $max = $_POST['meta_value'][1];
            $value = $min . ":" . $max;
            $value = json_encode(array($min, $max));
            $extra_value = $_POST['extra_value'];
            //}
        } else {
            $meta_val_array = isset($_POST['meta_value']) ? $_POST['meta_value'] : array();
            $newArray = array();
            foreach($meta_val_array as $meta_val_array_item){
                $newArray[] = str_replace("'","&apos;", str_replace("\\","",$meta_val_array_item));
            }
            $value = json_encode($newArray);
        }
        $sql = "INSERT INTO post_meta(post_id,post_type,meta_value,extra_value) VALUES(?,?,?,?) ";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(1, $post_id, PDO::PARAM_STR);
        $stmt->bindValue(2, $_POST['post_type'], PDO::PARAM_STR);
        $stmt->bindValue(3, $value, PDO::PARAM_STR);
        $stmt->bindValue(4, $extra_value, PDO::PARAM_STR);
        $stmt = $stmt->execute();
    }

    /*end insert post meta*/
    /*insert answer meta*/

    public function insertanswer($answer)
    {

        $sql = "INSERT INTO post_answer(user_id,meta_id,create_date, value) VALUES(?,?,?,?) ";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(1, $_POST['user_id'], PDO::PARAM_INT);
//        $stmt->bindValue(1, $_SESSION['authenticated'], PDO::PARAM_STR);
        $stmt->bindValue(2, $answer['meta_id'], PDO::PARAM_STR);
        $stmt->bindValue(3, strtotime(date("d-m-y", time())), PDO::PARAM_STR);
        $stmt->bindValue(4, $answer['data_value'], PDO::PARAM_STR);
        $stmt = $stmt->execute();
        $id_answer = $this->db->lastInsertId($stmt);

        $check = self :: checksendInteraction($_SESSION['authenticated'],$answer['id_reply'], 4);
       if($check == 0){
           self :: sendInteraction($answer['user_create'], $_SESSION['authenticated'], $answer['id_reply'], 4);
       }

        $count_answer = $this->gettanswerCount($answer['id_reply']);

        $count_answer['data_type'] = $answer['data_type'];
        $count_answer['post_id'] = $answer['id_reply'];
        return $count_answer;
    }



    /*end insert answer meta*/

    /*get count */
    /*insert answer meta*/



    public function gettanswerCount($post_id)
    {

        //count list one item commnet
        $sql_str = "SELECT 	p.id AS post_id,
                            pm.id AS meta_id,
                            pa.id AS post_answer_id,
                            pa.value AS post_answer_value,
                            pa.create_date AS post_answer_create_date,
                            COUNT( * ) AS count
                    FROM 	posts p, post_meta pm,
                        post_answer pa
                    WHERE 	p.id = pm.post_id
                        AND pm.id = pa.meta_id
                        AND p.id =".$post_id."
                    GROUP BY pa.value";
        $sql = $this->db->prepare($sql_str);
        $sql->execute();
        //end count list one item
        $sql_str_1 = "SELECT
                           COUNT( pm.id ) count_answer
                    FROM   post_answer pa,
                           post_meta pm,
                           posts p
                    WHERE p.id = pm.post_id
                    AND pa.meta_id = pm.id
                    AND pm.post_id = " . $post_id . "";

        $sql_1 = $this->db->prepare($sql_str_1);

        $sql_1->execute();

        $array_return = array(
            'total' => $sql_1->fetchall(PDO::FETCH_ASSOC),
            'list' => $sql->fetchall(PDO::FETCH_ASSOC)

        );

        return $array_return;

    }
    function favoriteCount($postId){
        $sql_str = "SELECT count(*) as fav_count FROM favorites WHERE id_favorite = '".$postId."'";
        $sql = $this->db->prepare($sql_str);
        $sql->execute();

        return $sql->fetchAll(PDO::FETCH_ASSOC);
    }




    function getAveragePostAnswerSlider($post_id)
    {
        $str_sql = "SELECT p.id AS post_id,
                            pm.id AS meta_id,
                            pa.id AS post_answer_id,
                            pa.value AS post_answer_value,
                            pa.create_date AS post_answer_create_date,
                            COUNT( * ) AS count,
                            AVG(pa.value) AS avg
                    FROM 	posts p, post_meta pm,
                        post_answer pa
                    WHERE 	p.id = pm.post_id
                        AND pm.id = pa.meta_id
                        AND p.id =".$post_id;
        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        return $rs;
    }


    /**/



    function listAllAnswerByUserId($user_id){


        $sql = $this->db->prepare("SELECT *
        FROM  `post_answer` pa, post_meta pm
        WHERE pa.meta_id = pm.id
        AND pa.user_id =" . $user_id);


        $sql->execute();


        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);


        return $rs;


    }

    function OpinionPost($user_id){
        $str_sql = 'SELECT *
                    FROM
                        posts p,
                        post_meta pm,
                        post_answer pa
                    WHERE
                        pm.post_id = p.id
                            AND pa.meta_id = pm.id
                            AND pa.user_id = '.$user_id.'
                    GROUP BY p.id';
        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        return $rs;
    }

    function findUserByEmail($email){
        $str_sql = "SELECT *
                    FROM
                        users
                    WHERE
                      email =  '".$email."'";
        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        return $rs;
    }

    function signUpWithFb($fbUser = array()){
        $sql = " INSERT INTO users
        (username,
        name,
        email,
         avatar,
         mode,
         status,
         language
        ) VALUES(
         '".$fbUser['username']."',
         '".$fbUser['name']."',
         '".$fbUser['email']."',
         '".$fbUser['avatar']."',
         '".$fbUser['mode']."',
         '".$fbUser['status']."',
         '".$fbUser['language']."'
        ) ";

        $stmt = $this->db->exec($sql);
        $user_id = $this->db->lastInsertId($stmt);

        return $user_id;

    }

    function checkAnswered($meta_id, $user_id){
        $str_sql = "SELECT count(*) AS count_check, id AS postanswerid
                    FROM
                        post_answer
                    WHERE
                      meta_id =  '".$meta_id."' AND user_id = ".$user_id;

        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        return $rs;
    }

    public function getSetting(){
        $str_sql = "SELECT * FROM admin_settings";
        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        return $rs;
    }

    public function getVoter($postId){
        $str_sql = 'SELECT pa.id AS post_answer_id,
                           pa.create_date AS post_answer_create_date,
                           pm.post_id AS post_id,
                           pa.value AS user_answer_value,
                           u.id AS user_id,
                           u.name AS user_real_name,
                           u.username AS user_name,
                           u.email AS user_email,
                           u.avatar AS user_avatar,
                           u.sex AS sex,
                           u.birth AS birth
                    FROM  post_answer pa,
                          post_meta pm,
                          users u
                    WHERE pa.meta_id = pm.id
                          AND pa.user_id = u.id
                          AND pm.post_id = '.$postId;
        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        return $rs;
    }

    public function getStatSexVoter($postId){
        $str_sql = 'SELECT COUNT(*) AS countSex, sex
                    FROM  post_answer pa,
                          post_meta pm,
                          users u
                    WHERE pa.meta_id = pm.id
                          AND pa.user_id = u.id
                          AND pm.post_id = '.$postId.'
                    GROUP BY sex';
        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        $returnArray = array();
        foreach($rs as $item){
            $count = $item['countSex'];
            $sex = $item['sex'];
            $returnArray[$sex] = $count;
        }
        return $returnArray;
    }


    public function getStatAgeVoter($postId){
        $str_sql = 'SELECT birth
                    FROM  post_answer pa,
                          post_meta pm,
                          users u
                    WHERE pa.meta_id = pm.id
                          AND pa.user_id = u.id
                          AND pm.post_id = '.$postId;
        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        $returnArray = array(
            'children' => 0, //5-12 Years
            'teens' => 0,  //13-18 Years
            'youth' => 0, // 19-24 Years
            'young-adults' => 0, //25-34 Years
            'adults' => 0 , //35-64 Years
            'senior' => 0 //64++
        );
        $curentYear = date('Y');
        foreach($rs as $item){
            $memYear = date('Y', $item['birth']);
            $age = $curentYear - $memYear;
            if($age >= 5 && $age <= 12){
                $returnArray['children'] ++;
            }
            if($age >= 13 && $age <= 18){
                $returnArray['teens'] ++;
            }
            if($age >=19 && $age <= 24){
                $returnArray['youth'] ++;
            }
            if($age >=25 && $age <= 34){
                $returnArray['young-adults'] ++;
            }
            if($age >=35 && $age <= 64){
                $returnArray['adults'] ++;
            }
            if($age >=65 ){
                $returnArray['senior'] ++;
            }
        }
        return $returnArray;
    }

    /**
     * @param $dataText
     * @param $idPost
     * @param null $createIdChart
     * @param array $option
     * @return string
     *
     */
    public  function renderPieChart($dataText, $idPost, $createIdChart = null, $option = array()){
//        echo $dataText;
//        echo '<br/>';
        $title = isset($option['title'])?$option['title']:'';
        $subtitle = isset($option['subtitle'])?$option['subtitle']:'';
        $showInLegend = isset($option['showInLegend'])?$option['showInLegend']:'true';
        $plotShadow = isset($option['plotShadow'])?$option['plotShadow']:'false';
        $plotBorderWidth = isset($option['plotBorderWidth'])?$option['plotBorderWidth']:'null';
        $plotBackgroundColor = isset($option['plotBackgroundColor'])?$option['plotBackgroundColor']:'null';
        $dataLabels = isset($option['dataLabels'])?$option['dataLabels']:'false';
        $width = isset($option['width'])?$option['width']:'null';
        $height= isset($option['height'])?$option['height']:'250';
        $optionColor= isset($option['color2'])?$option['color2']:'';
        if($optionColor != ''){

            $optionColor = '  Highcharts.setOptions({
                    colors: [\'#56B4EF\', \'#FFC0CB\']
                 });';
        }else{

            $optionColor = '  Highcharts.setOptions({
                      colors: [\'#50B432\', \'#ED561B\', \'#DDDF00\', \'#24CBE5\', \'#64E572\', \'#FF9655\', \'#FFF263\',      \'#6AF9C4\']
                 });';
        }
        $html = '
            <script type="text/javascript">
            $(function () {
                '.$optionColor.'
                $("#'.$createIdChart.'-' . $idPost . '").highcharts({
                        chart: {
                            plotBackgroundColor: '.$plotBackgroundColor.',
                            plotBorderWidth: '.$plotBorderWidth.',//null,
                            plotShadow: '.$plotShadow.',
                            height: '.$height.',
                            width: '.$width.'
                        },
                        title: {
                                            text: "'.$title.'"
                        },
                        tooltip: {
                                            pointFormat: "{series.name}: <b>{point.percentage:.1f}%</b>"
                        },

                        plotOptions: {
                                            pie: {
                                                allowPointSelect: true,
                                            cursor: "pointer",
                                            showInLegend: '.$showInLegend.',
                                            dataLabels: {
                                                    enabled: '.$dataLabels.',
                                                    format: "<b>{point.name}</b>: {point.percentage:.1f} %",
                                                    style: {
                                                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || "black"
                                                    }
                                            }
                        }
                                        },
                        series: [{
                                            type: "pie",
                                            width: 200,
                        name: "'.$subtitle.'",
                        data: [
                           '.$dataText.'
                        ]
                        }]
                        });
                        });
            </script>';
             $html .= '<div id="'.$createIdChart.'-' . $idPost . '"></div>';
        return $html;
    }

    public function countReply($postID){
        $str_sql = 'SELECT count(*) as countReply
                    FROM  reply r,users u
                    WHERE r.post = '.$postID.' AND r.status = \'1\' AND u.id = r.user AND u.status ="active"';



        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        return $rs;
    }

    /***
     * @param $postId
     * @param $postQuestions //list all meta answer
     * @param $array_info //list all answer answered
     * @return string
     */
    public function renderBarChart($postId,$postQuestions,$array_info){


        $seriesData = '';
        foreach($postQuestions as $postQuestion){
            $count = 0;
            $percent = 0;
            foreach($array_info as $info){
                if($info['title'] == $postQuestion){
                    $count = $info['value'];
                    $percent = $info['percent'];
                }
            }
            $seriesData .= '["'.$postQuestion.': '.$count.' vote(s) ",'.$percent.'],';
        }
        //render chart
        $chartContent = '
<script type="text/javascript">
        $(function () {
            setTimeout(function(){
                Highcharts.data({
                    csv: document.getElementById(\'tsv\').innerHTML,
                    itemDelimiter: "",
                    parsed: function (columns) {

                        var brands = {},
                            brandsData = [],
                            versions = {},
                            drilldownSeries = [];

                        // Parse percentage strings
                        columns[1] = $.map(columns[1], function (value) {
                            if (value.indexOf(\'%\') === value.length - 1) {
                                value = parseFloat(value);
                            }
                            return value;
                        });

                        $.each(columns[0], function (i, name) {
                            var brand,
                                version;

                            if (i > 0) {

                                // Remove special edition notes
                                name = name.split(\' -\')[0];

                                // Split into brand and version
                                version = name.match(/([0-9]+[\.0-9x]*)/);
                                if (version) {
                                    version = version[0];
                                }
                                brand = name.replace(version, \'\');

                                // Create the main data
                                if (!brands[brand]) {
                                    brands[brand] = columns[1][i];
                                } else {
                                    brands[brand] += columns[1][i];
                                }

                                // Create the version data
                                if (version !== null) {
                                    if (!versions[brand]) {
                                        versions[brand] = [];
                                    }
                                    versions[brand].push([version, columns[1][i]]);
                                }
                            }

                        });

                        $.each(brands, function (name, y) {
                            brandsData.push({
                                name: name,
                                y: y,
                                drilldown: versions[name] ? name : null
                            });
                        });
                        $.each(versions, function (key, value) {
                            drilldownSeries.push({
                                name: key,
                                id: key,
                                data: value
                            });
                        });

                        // Create the chart
                        $(\'#container-'.$postId.'\').highcharts({
                            chart: {
                                type: \'column\'
                            },
                            title: {
                                text: \'Count vote of post\'
                            },
                            subtitle: {
                                text: ""
                            },
                            xAxis: {
                                type: \'category\',

                            },
                            yAxis: {
                                title: {
                                    text: \'\'
                                },
                                labels : {
                                    enabled: false,
                                },
                                gridLineWidth : 0
                            },
                            legend: {
                                enabled: false
                            },
                            plotOptions: {
                                series: {
                                    borderWidth: 0,
                                    dataLabels: {
                                        enabled: true,
                                        format: \'{point.y:.1f}%\'
                                    }
                                }
                            },

                            tooltip: {
                                headerFormat: \'<span style="font-size:11px">{series.name}</span><br>\',
                                pointFormat: \'<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>\'
                            },

                            series: [{
                                name: "Brands",
                                colorByPoint: true,
                                data: ['.$seriesData.'

                                ]
                            }],
                            drilldown: {
                                series: drilldownSeries
                            }
                        })

                    }
                });
            }, 0);
        });
    </script>
<div id="container-'.$postId.'" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<!-- Data from www.netmarketshare.com. Select Browsers => Desktop share by version. Download as tsv. -->
<pre id="tsv" style="display:none">Browser Version	Total Market Share</pre>
<style>
    #container-'.$postId.'{min-width:585px!important}
</style>';

        return $chartContent;

    }

    public function reportPostCustom($postId, $userId)
    {
        /*
	 * ------------------------
	 * Report Post
	 * @$reported : verify if you have not reported
	 * @$verified: Check if the publication exists
	 * ----------------------
	 */

        $reported = self :: checkReportPost($userId, $postId) ? 1 : 0;

            if ($reported == 0) {
                $sql = $this->db->prepare("INSERT INTO post_reported VALUES( null, :_user, :_post, '" . $this->_dateNow . "' );");
                $sql->execute(array(':_user' => $userId, ':_post' => $postId));

                if ($sql->rowCount() != 0) {
                    return (1);
                } else {
                    return false;
                }
                $this->db = null;
            } /* If the user already reported the publication */
            else {
                return (3);
            }


    }

    public function deletePostCus($postId){

        $sql = $this->db->prepare("UPDATE posts
            SET status = '0'  WHERE id = :_id");
        $sql->execute(array(':_id'=>$postId));

    }

    public function redoPost($postanswerId){
        $sql = $this->db->prepare("DELETE FROM post_answer WHERE id = :_id");
        $sql->execute(array(':_id'=>$postanswerId));
    }

    public function getOnePost($id = null){
        $sql = $this->db->query(" SELECT   *  FROM posts WHERE id = ".$id);
        return $sql->fetchall();
    }

    public function getAds($pos){
        $str = " SELECT   *  FROM ads WHERE position  = '".$pos."'";
        echo 1;
        echo $str;
        die;
        $sql = $this->db->query($str);
        return $sql->fetchall();
    }

    public function getAllPostsCus($_where, $_limit, $session)
    {
        /*
			 * -----------------------------
			 * Get all Post in "Index page"
			 * $_where : Condition
			 * $_limit : Limit of post
			 * $_session: Current Session
			 * -----------------------------
			 */
        $sql = $this->db->prepare("
			SELECT
			COUNT(DISTINCT FA.id ) favoriteUser,
			U.username,
			P.repost_of_id,
			P.id,
			P.token_id,
			P.post_details,
			P.photo,
			P.video_code,
			P.video_title,
			P.video_site,
			P.video_url,
			P.user,
			P.date,
			P.url_soundcloud,
			P.title_soundcloud,
			U.id user_id,
			U.name,
			U.username,
			U.avatar,
			U.type_account,
			U.mode,
			U2.id rp_id_user,
			U2.name rp_name,
			U2.username rp_username,
			U2.avatar rp_avatar,
			U2.type_account rp_type_account,
			U2.mode rp_mode,
			RP.id rp_id,
			RP.user rp_user
			FROM posts P
			LEFT JOIN users U ON P.user = U.id
			LEFT JOIN posts RP ON P.repost_of_id = RP.id
			LEFT JOIN users U2 ON RP.user = U2.id
			LEFT JOIN favorites  FA ON RP.id = FA.id_favorite
			&& FA.id_usr = :id
			&& FA.status = '1'
			|| P.id = FA.id_favorite
			&& FA.id_usr = :id
			&& FA.status = '1'
			" . $_where . "
			" . $_limit . "
			");


//        echo "
//			SELECT
//			COUNT(DISTINCT FA.id ) favoriteUser,
//			U.username,
//			P.repost_of_id,
//			P.id,
//			P.token_id,
//			P.post_details,
//			P.photo,
//			P.video_code,
//			P.video_title,
//			P.video_site,
//			P.video_url,
//			P.user,
//			P.date,
//			P.url_soundcloud,
//			P.title_soundcloud,
//			U.id user_id,
//			U.name,
//			U.username,
//			U.avatar,
//			U.type_account,
//			U.mode,
//			U2.id rp_id_user,
//			U2.name rp_name,
//			U2.username rp_username,
//			U2.avatar rp_avatar,
//			U2.type_account rp_type_account,
//			U2.mode rp_mode,
//			RP.id rp_id,
//			RP.user rp_user
//			FROM posts P
//			LEFT JOIN users U ON P.user = U.id
//			LEFT JOIN posts RP ON P.repost_of_id = RP.id
//			LEFT JOIN users U2 ON RP.user = U2.id
//			LEFT JOIN favorites  FA ON RP.id = FA.id_favorite
//			&& FA.id_usr = :id
//			&& FA.status = '1'
//			|| P.id = FA.id_favorite
//			&& FA.id_usr = :id
//			&& FA.status = '1'
//			" . $_where . "
//			" . $_limit . "
//			";

        if ($sql->execute(array(':id' => $session))) {
            return $sql->fetchall(PDO::FETCH_ASSOC);
        }

    }

    public function countVote($userId){

        /*
        * -----------------------
        * get Admin Users
        * -----------------------
        */
        $sql = $this->db->query("
        SELECT count(*) as count_vote FROM post_answer  WHERE user_id = '".$userId."'      ");
        return $sql->fetchall();
    }

    public function getcategory($id){
      $str =  'SELECT * FROM category WHERE id = '.$id;
        $sql = $this->db->query($str);
       return $sql->fetchAll();
    }

    public function sliderSolid($postId,$chart,$val,$title,$subfix, $min, $max){



        $html ="
            <script type=\"text/javascript\">
                $(function () {

                    var gaugeOptions = {

                        chart: {
                            type: 'solidgauge'
                        },

                        title: null,

                        pane: {
                            center: ['50%', '85%'],
                            size: '140%',
                            startAngle: -90,
                            endAngle: 90,
                            background: {
                                backgroundColor: (Highcharts.theme && Highcharts.theme.background2) || '#EEE',
                                innerRadius: '60%',
                                outerRadius: '100%',
                                shape: 'arc'
                            }
                        },

                        tooltip: {
                            enabled: false
                        },

                        // the value axis
                        yAxis: {
                            stops: [
                                [0.1, '#55BF3B'], // green
                                [0.5, '#DDDF0D'], // yellow
                                [0.9, '#DF5353'] // red
                            ],

                            title: {
                                y: -70
                            },
                            labels: {
                                y: 16
                            }
                        },

                        plotOptions: {
                            solidgauge: {
                                dataLabels: false
                            }
                        }
                    };

                    // The speed gauge
                    $('#".$chart."-".$postId."').highcharts(Highcharts.merge(gaugeOptions, {
                        yAxis: {
                            min: ".$min.",
                            max: ".$max.",
                            title: {
                                text: '".$title."'
                            }
                        },

                        credits: {
                            enabled: false
                        },

                        series: [{
                            name: 'Speed',
                            data: [".str_replace(',','',$val)."],
                            dataLabels: {
                                format: '<div style=\"text-align:center\"><span style=\"font-size:25px;color:' +
                                ((Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black') + '\">{y}</span><br/>' +
                                '<span style=\"font-size:12px;color:silver\">".$subfix."</span></div>'
                            },
                            tooltip: {
                                valueSuffix: 'Prefix'
                            }
                        }]

                    }));
                    var chart = $('#".$chart."-".$postId."').highcharts();
                });
            </script>

        <div style=\"width: 300px; height: 200px; margin: 0 auto\">
            <div id=\"".$chart."-".$postId."\" style=\"width: 300px; height: 200px; float: left\"></div>
        </div>";
        return $html;
    }

    function alphabetconvert($phrase, $maxLength = 100000000000000)
    {
        $from = array('À', 'Á', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ằ', 'Ắ', 'Ẳ', 'Ẵ', 'Ặ', 'Â', 'Ầ', 'Ấ', 'Ẩ', 'Ẫ', 'Ậ',
            'à', 'á', 'ả', 'ã', 'ạ', 'â', 'ầ', 'ấ', 'ẩ', 'ẫ', 'ậ', 'ă', 'ằ', 'ắ', 'ẳ', 'ẵ', 'ặ',
            'Ò', 'Ó', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ồ', 'Ố', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ờ', 'Ớ', 'Ở', 'Ỡ', 'Ợ',
            'ò', 'ó', 'ỏ', 'õ', 'ọ', 'ô', 'ồ', 'ố', 'ổ', 'ỗ', 'ộ', 'ơ', 'ờ', 'ớ', 'ở', 'ỡ', 'ợ',
            'Ù', 'Ú', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ừ', 'Ứ', 'Ử', 'Ữ', 'Ự',
            'ù', 'ú', 'ủ', 'ũ', 'ụ', 'ư', 'ừ', 'ứ', 'ử', 'ữ', 'ự',
            'È', 'É', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê', 'Ề', 'Ế', 'Ể', 'Ễ', 'Ệ',
            'è', 'é', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ề', 'ế', 'ể', 'ễ', 'ệ',
            'Ì', 'Í', 'Ỉ', 'Ĩ', 'Ị',
            'ì', 'í', 'ỉ', 'ị', 'ĩ',
            'Ỳ', 'Ý', 'Ỉ', 'Ỹ', 'Ỵ',
            'ỳ', 'ý', 'ỷ', 'ỵ', 'ỹ',
            '.','/','"\"','%'
        );

        $to = array('A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
            'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a',
            'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O',
            'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o',
            'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U', 'U',
            'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u',
            'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E',
            'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e',
            'I', 'I', 'I', 'I', 'I',
            'i', 'i', 'i', 'i', 'i',
            'Y', 'Y', 'Y', 'Y', 'Y',
            'y', 'y', 'y', 'y', 'y',
            '_','_' ,'_','-'
        );


        $newstring = str_replace($from, $to, $phrase);

        $result = strtolower($newstring);

        $result = preg_replace("/[^A-Za-z0-9\s-._\/]/", "", $newstring);
        $result = trim(preg_replace("/[\s-]+/", " ", $newstring));
        $result = trim(substr($newstring, 0, $maxLength));
        $result = preg_replace("/\s/", "-", $newstring);

        return $result;

    }

    function checkUserReplay($userID, $postID){
        $str_sql = "SELECT count(*) AS count_check
                    FROM
                        post_answer pa, post_meta pm
                    WHERE
                      pa.meta_id = pm.id AND
                      pm.post_id =  '".$postID."' AND user_id = ".$userID;
//        echo $str_sql;

        $sql = $this->db->prepare($str_sql);
        $sql->execute();
        $rs = $sql->fetchAll(PDO::FETCH_ASSOC);
        return $rs;
    }
}

//*************************************** End Class AjaxRequest() *****************************************//
?>